Search code examples
scalahttp4s

HTTP4S client. How to get the exact request and response body


I am writing a small http4s client

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"
}

The output is like

[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

How to convert the binary request body to String? I want to see the body and headers in clear text.


Solution

  • I had a conversation with the http4s team on gitter and found the response. since gitter talk is not returned by google I am putting the answer here

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

    this prints all the headers. If we do something with the loggedReq then we get the entire body which is posted

    loggedReq.as[String].run