jsonf#gzipsuave

How enable compression for a JSON REST call in Suave?


I want to know how return JSON for my REST calls compressed:

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

But the docs only talk about returning files

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

Solution

  • I believe this is configured in the MIME type maps by adding a new MIME type and passing true for the second parameter, like this:

    let mimeTypes =
      defaultMimeTypesMap
        @@ (function | ".json" -> createMimeType "application/json" true | _ -> None)
    
    let webConfig = { defaultConfig with mimeTypesMap = mimeTypes }
    

    The configuration for MIME types and compression is documented on this page.