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

How enable compression for a JSON REST call in Suave?

我想知道如何 return JSON 压缩我的 REST 调用:

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

但文档只讨论 returning 文件

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))

我相信这是通过添加新的 MIME 类型并为第二个参数传递 true 在 MIME 类型映射中配置的,如下所示:

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

let webConfig = { defaultConfig with mimeTypesMap = mimeTypes }

MIME 类型和压缩的配置记录在 this page