【问题标题】:HTTP4S client. How to get the exact request and response bodyHTTP4S 客户端。如何获取准确的请求和响应正文
【发布时间】:2017-01-28 07:01:01
【问题描述】:

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

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

【问题讨论】:

    标签: scala http4s


    【解决方案1】:

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

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

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

    loggedReq.as[String].run
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 2011-02-01
      • 2012-11-26
      相关资源
      最近更新 更多