【问题标题】:Bad request data: "Requests with 'Connection: Upgrade' cannot have content in the request body." when doing POST request to APi错误的请求数据:“带有‘连接:升级’的请求不能在请求正文中包含内容。”向 APi 发出 POST 请求时
【发布时间】:2023-03-21 17:10:01
【问题描述】:

我创建了一个简单的 .NET 核心 Web API,它可以在 localhost 上完美运行。 每当我将 docker 映像部署到我们的服务器(Jelastic)并 POST 到唯一的 POST 端点时,我都会失败。

错误如下:

 INFO  02:41:14 Connection id "0HLOLOJQ1QFRM" bad request data: "Requests with 'Connection: Upgrade' cannot have content in the request body."
Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Requests with 'Connection: Upgrade' cannot have content in the request body.
   at Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException.Throw(RequestRejectionReason reason)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1MessageBody.For(HttpVersion httpVersion, HttpRequestHeaders headers, Http1Connection context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.CreateMessageBody()
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync[TContext](IHttpApplication`1 application)

所有 GET 请求都成功成功。

[HttpPost]
public ActionResult<List<string>> Post([FromBody]Dictionary<string, string> data)
{
    //LOGIC
}

我认为没有反向代理(NGINX 或类似的),一切都由 Kestrel 处理。

我的要求是:

POST /api/word HTTP/1.1
Host: *our server*
Content-Type: application/json
User-Agent: PostmanRuntime/7.15.2
Accept: */*
Cache-Control: no-cache
Postman-Token: 9d575404-6347-4783-a03f-cba29f9113e8,b8a94c5d-1ee5-42c4-a2e4-348c9af0e064
Host: *our server*
Accept-Encoding: gzip, deflate
Content-Length: 110
Connection: keep-alive
cache-control: no-cache

{
"customerName": "test-entries",
"reportPeriod": "test-entries"
}

使用 CURL 时也会失败。 这里有什么可以忽略的?

【问题讨论】:

  • 你确定它在本地有效吗?对于[FromBody],它期望使用Content-Type: application/json 发送请求,但您使用Content-Type: text/plain 发送请求。
  • 感谢您的回复,看来我从我们的测试集中收到了错误的请求,我已经更新了请求,但结果仍然没有区别。
  • 您目前的要求是什么?
  • 您确定 Jelastic 没有运行反向代理?
  • @mjwills 确实如此。

标签: c# docker asp.net-core .net-core kestrel-http-server


【解决方案1】:

正如@mjwills 在 cmets 中所说,它似乎确实是一个 Jelastic 代理。 在 Jelastic 中,我注册了一个端点,将容器中的端口 80 公开映射到 11000,问题似乎解决了。 Jelastic 中的代理/映射/重新路由似乎改变了请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    相关资源
    最近更新 更多