Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 462
Vagif Abilov Avatar asked Aug 29 '16 14:08

Vagif Abilov


People also ask

How do I configure F buttons?

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.

How do I not press Fn for F keys?

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.

How do I set the Fn key always on?

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.


1 Answers

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.

like image 158
Tomas Petricek Avatar answered Oct 12 '22 21:10

Tomas Petricek