F#- Using HttpFs.Client and Hopac: How do I get a response code, response headers and response cookies?

428 views Asked by At

I am using F# with HttpFs.Client and Hopac.

I am able to get Response body and value of each node of JSON/XML response by using code like:

[<Test>]
let ``Test a Create user API``() = 
    let response = Request.createUrl Post "https://reqres.in/api/users"
                   |> Request.setHeader (Accept "application/json")
                   |> Request.bodyString ReadFile
                   |> Request.responseAsString
                   |> run



        printfn "Response of get is %s: " response
        let info = JsonValue.Parse(response)
        let ID = info?id

        printfn "ID in Response is %i: " (ID.AsInteger())

But how do I get a response code, response headers, and response cookies? I need to get this inside the same method as shown above so that I can do the assertion on these items too.

I did try response.StatusCode, response.Cookies.["cookie1"] but there are no such methods comes up when I add period after response.

1

There are 1 answers

4
Koenig Lear On BEST ANSWER
let response = 
    Request.createUrl Post "https://reqres.in/api/users"
    |> Request.setHeader (ContentType (ContentType.create("application", "json")))
    |> Request.bodyString token //Reading content of json body
    |> HttpFs.Client.getResponse
    |> run

Please read the doc https://github.com/haf/Http.fs

Point 3 shows how to access cookies and headers in the response.

response.StatusCode
response.Body // but prefer the above helper functions
response.ContentLength
response.Cookies.["cookie1"]
response.Headers.[ContentEncoding]
response.Headers.[NonStandard("X-New-Fangled-Header")]