Reputation: 9991
I need to encode as UTF-8 HTTP payloads sent using F# JSON type provider JsonValue.Request method.
The Request allows specifying HTTP headers but JSON type provider takes care of setting Content-Type header as "application/json" without specifying "charset" qualifier. If I try to set content type myself I end up with duplicate header value (causing an error indeed). And with only "application/json" the following code selects 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 is ISO-8859-1 which is not suitable in our scenario.
Any idea how PostDefaultEncoding can be overridden for JsonValue.Request payload?
Upvotes: 6
Views: 956
Reputation: 243061
I think the JsonValue.Request
method is just a convenient helper that covers the most common scenarios, but does not necessarily give you all the power you need for making arbitrary requests. (That said, UTF-8 sounds like a more reasonable default.)
The most general F# Data function is Http.Request
, which lets you upload 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 the request with UTF-8 encoded body.
Upvotes: 4