喷雾解压缩HttpResponse [英] Spray Unzip HttpResponse

查看:135
本文介绍了喷雾解压缩HttpResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spray API(spray客户端)访问外部URL,并且压缩了HttpResponse.如何解压缩此HttpResponse以获取其实体(在我的情况下为json)?

I'm using Spray API(spray-client) to hit an external URL and I'm getting gzipped HttpResponse. How do I unzip this HttpResponse to get its entity(json, in my case)?

val future: Future[HttpResponse] = (IO(Http) ? Get(uri)).mapTo[HttpResponse]
val response = Await.result(future, Duration.inf)
val json = response.entity

在这里,将json压缩.如何解压缩?

Here, json is gzipped. How do I unzip it?

推荐答案

您需要使用流水线和decode指令.就像在这个示例中一样.

You need to use pipelining and the decode directive. Like in this example.

修改该示例,您的代码将如下所示:

Modifying that example your code would look something like this:

val pipeline: HttpRequest => Future[String] = (
  sendReceive
  ~> decode(Gzip)
  ~> unmarshal[String]
)
val response: Future[String] =
  pipeline(Get(uri))

如果您不希望期货带来任何好处,则可以对响应进行等待".

You can then do Await on the response if you don't want the benefits of Futures.

在旁注中,您可以使用 spray-json 并为您的响应创建一个对象,然后解组http响应直接进入案例类,而无需处理json.

On a side note you can use spray-json and create an object for your response and then unmarshal the http response directly into a case class without having to deal with the json.

这篇关于喷雾解压缩HttpResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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