【发布时间】:2016-03-23 00:52:27
【问题描述】:
我正在使用一个旧的 HTTP API(我无法更改),它在正文中使用 JSON 进行响应,但会提供一个 Content-Type: text/plain; charset=utf-8 标头。
我正在尝试将 HTTP 正文解组为 JSON,但出现以下异常:akka.http.scaladsl.unmarshalling.Unmarshaller$UnsupportedContentTypeException: Unsupported Content-Type, supported: application/json
我的代码如下所示:
import spray.json.DefaultJsonProtocol
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.unmarshalling._
case class ResponseBody(status: String, error_msg: String)
object ResponseBodyJsonProtocol extends DefaultJsonProtocol {
implicit val responseBodyFormat = jsonFormat2(ResponseBody)
}
def parse(entity: HttpEntity): Future[ResponseBody] = {
implicit val materializer: Materializer = ActorMaterializer()
import ResponseBodyJsonProtocol._
Unmarshal[HttpEntity](entity).to[ResponseBody]
}
HTTP 响应示例如下所示:
HTTP/1.1 200 OK
Cache-Control: private
Content-Encoding: gzip
Content-Length: 161
Content-Type: text/plain; charset=utf-8
Date: Wed, 16 Dec 2015 18:15:14 GMT
Server: Microsoft-IIS/7.5
Vary: Accept-Encoding
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
{"status":"1","error_msg":"Missing parameter"}
如何忽略 HTTP 响应中的 Content-Type 并解析为 JSON?
【问题讨论】:
标签: json http parsing spray-json akka-http