Git Product home page Git Product logo

Comments (3)

RonaldSchlenker avatar RonaldSchlenker commented on June 4, 2024 1

In the past releases, json set the content type to text/json - see #93 .

In general, the API is built up in a way that defining inconsistent requests is hard or impossible. E.g. defining a content type on a request without content is not possible. Technically, this is done by changing the underlying types defining the computation: There is a HeaderContext for specifying HTTP header. That context can be transformed using the body operation into a BodyContext or into a MultipartContext using the multipart operation, etc. On each context, there is a set of possible operations (see here: https://github.com/fsprojects/FsHttp/blob/master/src/FsHttp/Dsl.CE.fs). That's the reason why ContentType has to be places into the body.

from fshttp.

RonaldSchlenker avatar RonaldSchlenker commented on June 4, 2024

Thanks for posting the issue - it's valuable to have issues like that.

Please see the comments:

#r "nuget: FsHttp"
open FsHttp

let getString url rjson = async {
    let! response =
        // See also: https://fsprojects.github.io/FsHttp/Migrations.html
        // 'httpAsync' is replaced by 'http { ... } |> Request.sendAsync'
        http {
            POST url
            Origin "Web3.fs"
            
            // a) 'ContentType' is an operation only valid
            //    on the reqiest 'body' definition.
            // b) Setting ContentType to 'application/json' is
            //    redundant when "json" operation is used.
            body
            ContentType "application/json"
            json rjson
            
            config_timeoutInSeconds 18.5
        }
        |> Request.sendAsync
    let! responseContent = response |> Response.toTextAsync
    return responseContent
}

// Instead of using 'asyn { ... }' CE, it sometimes is sufficient
// piping async 
let getStringAlternative url rjson =
    http {
        POST url
        Origin "Web3.fs"
            
        body
        json rjson
        ContentType "application/json"
            
        config_timeoutInSeconds 18.5
    }
    |> Request.sendAsync
    // use 'Async.await' that works like 'bind':
    |> Async.await Response.toTextAsync
    // use 'Async.map' to transform results in an async context:
    |> Async.map (fun text -> text.ToUpperInvariant())

from fshttp.

jmhickman avatar jmhickman commented on June 4, 2024

Excellent, thank you for the response. It wasn't obvious that the order of the items in the CE matters.
I seem to recall that, for my interaction, if I didn't explicitly set the ContentType I got weird behavior from my endpoint, but perhaps that won't be an issue in 9.0.x, I'll have a second look.

I appreciate the help.

from fshttp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.