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?
To enable it, we'd hold Fn and press the Esc key. To disable it, we'd hold Fn and press Esc again. It functions as a toggle just like Caps Lock does. Some keyboards may use other combinations for Fn Lock.
Method 1. Toggle the Fn Lock key All you have to do is look on your keyboard and search for any key with a padlock symbol on it. Once you've located this key, press the Fn key and the Fn Lock key at the same time. Now, you'll be able to use your Fn keys without having to press the Fn key to perform functions.
Enabling FN Lock on the All in One Media Keyboard To enable FN Lock on the All in One Media Keyboard, press the FN key, and the Caps Lock key at the same time. To disable FN Lock, press the FN key, and the Caps Lock key at the same time again.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With