Akka-HTTP:如何知道在收到的响应中是否明确设置了 Content-type 标头

问题描述

akka-http 中是否有一种方法可以知道我们收到的 HttpResponse 中是否明确设置了“Content-type”标头? 从嗅探到的 Http 转储中我看到,没有“Content-Type”标头,但是

httpResponse.header[`Content-Type`].get.contentType.mediaType.toString() httpResponse.entity.getContentType().mediaType.toString

仍然返回 application/octet-stream

解决方法

这不仅是 Akka-HTTP 的默认内容类型,也可能是其他框架(如 Play)的默认内容类型。 Akka-http 和其他基于 HTTP 的技术需要知道如何根据此标头在内部解析内容。 application/octet-stream 表示它将请求正文视为字节流。 经验法则:如果可能 - 尝试指定 Content-type