akka HttpResponse 将正文读取为字符串 scala

akka HttpResponse 将正文读取为字符串 scala

问题描述:

所以我有一个带有这个签名的函数(akka.http.model.HttpResponse):

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?

import akka.http.scaladsl.unmarshalling.Unmarshal

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

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