runtime: HTTP POST and PUT requests fail with "The response ended prematurely"

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When running an HTTP POST or HTTP PUT request using the HttpClient against an ASP.NET Core Web API, it fails with the following exception:

System.Net.Http.HttpRequestException: An error occurred while sending the request.

System.Net.Http.HttpRequestException
An error occurred while sending the request.
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)

System.IO.IOException
The response ended prematurely.
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)

This only happens under the following circumstances:

  • Extension method PutAsJsonAsync is used
  • Extension method PostAsJsonAsync is used
  • PostAsync with HttpContent of null is used
  • PostAsync with HttpContent of new StringContent(string.Empty) is used

These requests only fail when using the HttpClient in C#, but they succeed when running them via…

  • PowerShell
  • curl
  • Swagger

Expected Behavior

The HTTP PUT and POST requests do not fail.

Steps To Reproduce

  1. Clone this repository: https://github.com/mu88/Repro_HttpClient_POST_fails
  2. Open the *.sln
  3. Start the WebApi project
  4. Run the tests

In my case, the test result looks like this:
image

When checking the WebApi logs, I see that the backend seems to handle the requests properly - at least I always see a 2xx status code:

ASP.NET Core Logs (details dropdown)
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: POST
      Scheme: http
      PathBase:
      Path: /api/processor/process
      Host: localhost:5254
      Content-Type: application/json; charset=utf-8
      Content-Length: 50
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
      Response:
      StatusCode: 204
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: PUT
      Scheme: http
      PathBase:
      Path: /api/processor/process
      Host: localhost:5254
      Content-Type: application/json; charset=utf-8
      Content-Length: 50
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
      Response:
      StatusCode: 204
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: GET
      Scheme: http
      PathBase:
      Path: /api/processor/hello
      Host: localhost:5254
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
      Response:
      StatusCode: 200
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: DELETE
      Scheme: http
      PathBase:
      Path: /api/processor/goodbye
      Host: localhost:5254
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
      Response:
      StatusCode: 200
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: POST
      Scheme: http
      PathBase:
      Path: /internal-api/dataset/complete
      Host: localhost:5254
      Content-Length: 0
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
      Response:
      StatusCode: 204
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: POST
      Scheme: http
      PathBase:
      Path: /internal-api/dataset/complete
      Host: localhost:5254
      Content-Length: 0
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
      Response:
      StatusCode: 204
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: POST
      Scheme: http
      PathBase:
      Path: /internal-api/dataset/complete
      Host: localhost:5254
      Content-Length: 0
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
      Response:
      StatusCode: 204
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: POST
      Scheme: http
      PathBase:
      Path: /internal-api/dataset/complete
      Host: localhost:5254
      Content-Length: 0
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
      Response:
      StatusCode: 204
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: POST
      Scheme: http
      PathBase:
      Path: /internal-api/dataset/complete
      Host: localhost:5254
      Content-Type: text/plain; charset=utf-8
      Content-Length: 0
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
      Response:
      StatusCode: 204
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: PUT
      Scheme: http
      PathBase:
      Path: /api/processor/process
      Host: localhost:5254
      Content-Type: application/json; charset=utf-8
      Transfer-Encoding: chunked
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: POST
      Scheme: http
      PathBase:
      Path: /api/processor/process
      Host: localhost:5254
      Content-Type: application/json; charset=utf-8
      Transfer-Encoding: chunked
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
      Response:
      StatusCode: 200
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
      Response:
      StatusCode: 200

The tests are only used for easier usage. Instead, the CLI project Client can be used as well and triggers the same exceptions.

Exceptions (if any)

System.Net.Http.HttpRequestException: An error occurred while sending the request.

System.Net.Http.HttpRequestException
An error occurred while sending the request.
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
   at Client.Helper.CompleteViaPostWithEmptyAsync(HttpClient httpClient) in C:\work\GitHub\Repro_HttpClient_POST_fails\Client\Helper.cs:line 49

System.IO.IOException
The response ended prematurely.
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)

.NET Version

8.0.101

Anything else?

Additional information

  • ASP.NET Core 8
  • Happens in both Visual Studio and JetBrains Rider
  • Windows 10 22H2 (OS build 19045.3930)
  • Output of dotnet --info:
Dotnet info
.NET SDK:
 Version:           8.0.101
 Commit:            6eceda187b
 Workload version:  8.0.100-manifests.f64b8c85

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19045
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.101\

.NET workloads installed:
 Workload version: 8.0.100-manifests.f64b8c85
 [maui-maccatalyst]
   Installation Source: SDK 8.0.100
   Manifest Version:    8.0.3/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maui\8.0.3\WorkloadManifest.json
   Install Type:              Msi

 [maui-tizen]
   Installation Source: SDK 8.0.100
   Manifest Version:    8.0.3/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maui\8.0.3\WorkloadManifest.json
   Install Type:              Msi

 [maui-windows]
   Installation Source: VS 17.8.34408.163
   Manifest Version:    8.0.3/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maui\8.0.3\WorkloadManifest.json
   Install Type:              Msi

 [maccatalyst]
   Installation Source: VS 17.8.34408.163
   Manifest Version:    17.0.8490/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maccatalyst\17.0.8490\WorkloadManifest.json
   Install Type:              Msi

 [ios]
   Installation Source: VS 17.8.34408.163
   Manifest Version:    17.0.8490/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.ios\17.0.8490\WorkloadManifest.json
   Install Type:              Msi

 [android]
   Installation Source: VS 17.8.34408.163
   Manifest Version:    34.0.52/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.android\34.0.52\WorkloadManifest.json
   Install Type:              Msi


Host:
  Version:      8.0.1
  Architecture: x64
  Commit:       bf5e279d92

.NET SDKs installed:
  2.1.700 [C:\Program Files\dotnet\sdk]
  2.2.300 [C:\Program Files\dotnet\sdk]
  8.0.101 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found
  • It does not matter whether .NET 7 or 8 is used (can be controlled via Directory.Build.props) - the exception stays the same.
  • I asked several colleagues to reproduce the issue with my repro case on their machines, but without luck: at the moment, I’m the only one with this problem.
  • Maybe related to dotnet/aspnetcore#23949

Broader context

Of course, this is a rather simplified example. In our team, we’re maintaining a dozen of different apps following the same pattern which is similar to the shown example. Since we set great store by testing, we have a lot of system integration tests where we test our ASP.NET Core Web API (which is running in Docker) via HttpClient.

Since I got a new laptop, I installed all the necessary pieces (Git, .NET SDK, Docker Desktop, etc.) and cloned several of our apps. I can run all unit and integration tests, but some of the system integration tests fail. It took me a long time to remove the complexity step by step until I noticed that only those tests fail which make use HTTP POST or PUT using the HttpClient. So I further removed different layers (Docker, xUnit) and reduced it down to a dumb CLI app using the HttpClient to access an ASP.NET Core Web API - the result is the repro repo https://github.com/mu88/Repro_HttpClient_POST_fails.

❗ So at the moment, I’m not able to run any of our system integration tests that make use of HTTP POST or PUT. Therefore, this bug has a certain impact on my inner dev loop and I’d appreciate it if you could have a look.
I’m fully aware that you (as my colleagues) probably won’t be able to reproduce the problem. However, I hope that you can give me some guidance on what to analyze. Thanks in advance!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 17 (8 by maintainers)

Commits related to this issue

Most upvoted comments

I was finally able to resolve the issue: I’m running an instance of px proxy on my machine and stopping it broke the whole test suite, which confused me. So I started removing the env vars http_proxy and https_proxy and out of a sudden, the tests were all green ✅ So I set the no_proxy env var to localhost, 127.0.0.1, started px proxy and the tests were still all green.

Long story short: px proxy resp. not configuring no_proxy was the problem.

Thank you very much for your help! I really appreciate it that you gave me your precious time 🙏🏻