How to configure a provider like F # JSON to use UTF-8 for POST requests?

I need to encode UTF-8 as useful HTTP data using the provider JsonValue.Request method of type F # JSON.

The request allows you to specify HTTP headers, but the JSON provider will take care of setting the Content-Type header as "application / json" without specifying the "charset" qualifier. If I try to set the content type myself, I get a duplicate header value (causing an error). And only with the help of "application / json" does the following code select the defauls encoding:

let getEncoding contentType = let charset = charsetRegex.Match(contentType) if charset.Success then Encoding.GetEncoding charset.Groups.[1].Value else HttpEncodings.PostDefaultEncoding 

PostDefaultEncoding is an ISO-8859-1 that does not fit our scenario.

Any idea how PostDefaultEncoding can be overridden for the JsonValue.Request payload?

+5
source share
1 answer

I think the JsonValue.Request method is just a handy helper that covers the most common scenarios, but does not necessarily give you all the power you need for arbitrary queries. (However, UTF-8 sounds like a more reasonable default.)

The most common F # data function is Http.Request , which allows you to load data in any way:

 let buffer = System.Text.Encoding.UTF8.GetBytes "👍👍👍" Http.RequestString ( "https://httpbin.org/post", httpMethod="POST", body=HttpRequestBody.BinaryUpload buffer ) 

So, if you replace "👍👍👍" with yourJsonValue.ToString() , this should send a request with the encoded body of UTF-8.

+2
source

All Articles