akka HttpResponse将主体读取为String scala [英] akka HttpResponse read body as String scala

查看:117
本文介绍了akka HttpResponse将主体读取为String 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]

这篇关于akka HttpResponse将主体读取为String scala的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆