如何在Suave中为JSON REST调用启用压缩?

时间:2018-07-06 17:32:16

标签: json f# gzip suave

我想知道如何为压缩的REST调用返回JSON:

GET /orders HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate

但是文档只谈论返回文件

let setJson it =
    it
    |>JsonUtils.toJson
    |> OK
    >=> setMimeType "application/json; charset=utf-8"

let doReq route request action =
    path route >=> setCORSHeaders >=> request (fun r -> action(r) |> setJson)

doReq "/orders" request (fun r -> queryOrders(r |> getTerm, Products.Name))

1 个答案:

答案 0 :(得分:1)

我相信可以通过添加新的MIME类型并为第二个参数传递true来在MIME类型映射中进行配置,例如:

let mimeTypes =
  defaultMimeTypesMap
    @@ (function | ".json" -> createMimeType "application/json" true | _ -> None)

let webConfig = { defaultConfig with mimeTypesMap = mimeTypes }

this page中记录了MIME类型和压缩的配置。