我正在使用一个旧的 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"}
我该怎么做才能忽略Content-Type
HTTP 响应中的 并解析为 JSON?