我将F#与HttpFs.Client和Hopac结合使用。
通过使用以下代码,我能够获得JSON/XML响应的每个节点的响应主体和值:
[<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())但是如何获得响应代码、响应头和响应cookie?我需要在上面所示的相同的方法中得到这一点,这样我也可以对这些项进行断言。
我确实尝试过response.StatusCode,response.Cookies."cookie1“,但是当我在响应之后添加句点时,并没有出现这样的方法。
发布于 2020-06-12 11:12:33
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请阅读https://github.com/haf/Http.fs文档
第3点展示了如何在响应中访问cookie和头。
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")]https://stackoverflow.com/questions/62338294
复制相似问题