HTTP4S 客户端。如何获得准确的请求和响应body
HTTP4S client. How to get the exact request and response body
我正在写一个小型的 http4s 客户端
val client = SimpleHttp1Client()
val uri = Uri.fromString(requestUrl).valueOr(throw _)
val task = POST(uri, UrlForm("username" -> userName, "password" -> password)).map{request => println("request: " + request.body)}
try {
val response = client.expect[String](task).unsafePerformSync
println("token: " + response)
response
} catch {
case e: Exception => println(e.getMessage);"BadToken"
}
输出就像
[info] Running com.researchnow.nova.shield.NovaShieldSetup
[info] Emit(Vector(ByteVector(44 bytes, 0x757365726e616d653d616268737269766173746176612670617373776f72643d41726)))
[info] Failed: unexpected HTTP status: 400 Bad Request
[info] token: BadToken
如何将二进制请求body转换为字符串?我想以明文形式查看 body 和 headers。
我在 gitter 上与 http4s 团队进行了对话并找到了回应。因为 gitter talk 没有被 google 返回,所以我把答案放在这里
val loggedReq = req.copy(body = request.body.observe(scalaz.stream.io.stdOutBytes))
println(loggedReq)
这将打印所有 headers。如果我们对 loggedReq 做一些事情,那么我们会得到整个 body 并发布
loggedReq.as[String].run
我正在写一个小型的 http4s 客户端
val client = SimpleHttp1Client()
val uri = Uri.fromString(requestUrl).valueOr(throw _)
val task = POST(uri, UrlForm("username" -> userName, "password" -> password)).map{request => println("request: " + request.body)}
try {
val response = client.expect[String](task).unsafePerformSync
println("token: " + response)
response
} catch {
case e: Exception => println(e.getMessage);"BadToken"
}
输出就像
[info] Running com.researchnow.nova.shield.NovaShieldSetup
[info] Emit(Vector(ByteVector(44 bytes, 0x757365726e616d653d616268737269766173746176612670617373776f72643d41726)))
[info] Failed: unexpected HTTP status: 400 Bad Request
[info] token: BadToken
如何将二进制请求body转换为字符串?我想以明文形式查看 body 和 headers。
我在 gitter 上与 http4s 团队进行了对话并找到了回应。因为 gitter talk 没有被 google 返回,所以我把答案放在这里
val loggedReq = req.copy(body = request.body.observe(scalaz.stream.io.stdOutBytes))
println(loggedReq)
这将打印所有 headers。如果我们对 loggedReq 做一些事情,那么我们会得到整个 body 并发布
loggedReq.as[String].run