3

我正在写一个小的 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

如何将二进制请求正文转换为字符串?我想以明文形式查看正文和标题。

4

1 回答 1

2

我在 gitter 上与 http4s 团队进行了对话,并找到了响应。因为谷歌没有返回 gitter talk 我把答案放在这里

val loggedReq = req.copy(body = request.body.observe(scalaz.stream.io.stdOutBytes))
println(loggedReq)

这将打印所有标题。如果我们对loggedReq做一些事情,那么我们会得到整个发布的body

loggedReq.as[String].run
于 2017-01-27T23:01:01.970 回答