Search code examples
stringscalaapache-httpclient-4.xscalatrabytearrayinputstream

Scala ByteArrayInputStream to String


I'm trying to log the body of a post made using org.apache.http.client. I'm using Scalatra version 2.4.0.RC3 and Scala version 2.11.7. My response is a 400 Bad Request and I need to get the message provided in the response body.

Here is my current code:

val response = client.execute(post)
println(response)
println(response.getEntity().getContent())

response.getEntity().getContent() prints:

java.io.ByteArrayInputStream@1q232e4e

I need to get the actual body as string from this ByteArrayInputStream.


Solution

  • You can use EntityUtils form the same library:

    import org.apache.http.util.EntityUtils;
    println(EntityUtils.toString(response.getEntity()));