【问题标题】:Akka http Explicitly set HTTP header 'Content-Type: application/json' is ignoredAkka http 显式设置 HTTP 标头 'Content-Type: application/json' 被忽略
【发布时间】:2019-11-02 01:36:29
【问题描述】:

我有这样的代码:

val request = HttpRequest(
      method = HttpMethods.POST,
      uri = Uri(url))
      .withHeaders(List(authorization, ctJson))
      .withEntity(ContentTypes.`application/json`, ByteString(jsonStr))
 Http().singleRequest(request)

效果很好,但我在请求期间收到警告:

明确设置 HTTP 标头 'Content-Type: application/json' 是 忽略,不允许显式 Content-Type 标头。放 HttpRequest.entity.contentType 代替。

如何解决这个问题?我也试过了:

val request = HttpRequest(
      HttpMethods.POST,
      uri = "https://api.sendgrid.com/v3/mail/send",
      entity = HttpEntity(ContentTypes.`application/json`, ByteString(jsonStr)),
      headers = List(authorization, ctJson))

但结果相同..

【问题讨论】:

  • ctJson 标头是什么?如果是application/json,你就不需要了,这就是 Akka-Http 抱怨的地方。

标签: json scala akka-http


【解决方案1】:

如前所述,您向实体添加了正确的内容类型,但您需要删除 ctJson 标头。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-02
    • 2018-04-14
    • 2018-06-25
    • 2012-07-08
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    相关资源
    最近更新 更多