2

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.

  • are you sure you are using HttpFs.Client? from you code I think you are using this one https://fsharp.github.io/FSharp.Data/library/Http.html – Nghia Bui Jun 12 '20 at 06:46
  • @NghiaBui Thanks for the correction, I have made the changes to show HttpFs.Client example. Do let me know if you have a solution to extract a response code, response headers, and response cookies using HttpFs.Client. – Sanjay Kumar Jun 12 '20 at 09:02

1 Answers1

1
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")]
Koenig Lear
  • 2,366
  • 1
  • 14
  • 29
  • @koenigLear- I checked the doc and tried to apply but I don't see any such methods like StatusCode, cookies, etc. when I put response(period) after capturing response like let response = Request.createUrl Post "https://reqres.in/api/users" |> Request.setHeader (Accept "application/json") |> Request.bodyString ReadFile |> Request.responseAsString |> run – Sanjay Kumar Jun 13 '20 at 10:33
  • @SanjayKumar see sample above – Koenig Lear Jun 13 '20 at 10:53
  • Thanks for providing the code snippet. There is so much fuss about using whitespace and indentation in F#. Need to know how this JobBuilder works. – Sanjay Kumar Jun 13 '20 at 12:22
  • Please share, if there is any other way than creating code like a jobBuilder? – Sanjay Kumar Jun 13 '20 at 13:35