akka HttpResponse read body as String scala

tg44 picture tg44 · Aug 31, 2015 · Viewed 26.8k times · Source

So I have a function with this signature (akka.http.model.HttpResponse):

def apply(query: Seq[(String, String)], accept: String): HttpResponse

I simply get a value in a test like:

val resp = TagAPI(Seq.empty[(String, String)], api.acceptHeader)

I want to check its body in a test something like:

resp.entity.asString == "tags"

My question is how I can get the response body as string?

Answer

user3548738 picture user3548738 · Apr 10, 2016
import akka.http.scaladsl.unmarshalling.Unmarshal

implicit val system = ActorSystem("System")  
implicit val materializer = ActorFlowMaterializer() 

val responseAsString: Future[String] = Unmarshal(entity).to[String]