Body matching can cause "The response ended prematurely" errors #290

Closed
opened 2025-12-29 08:25:31 +01:00 by adam · 5 comments
Owner

Originally created by @ilia-cy on GitHub (Aug 14, 2020).

I'm using the mock server for some time now, and I recently started to encounter this strange issue.
All mocked responses work properly, except for ones that are attempting to respond to POST requests that contain something in the body.
These mocked responses fail our CI once in a while, as when the client performs the POST request it receives the following HttpRequestException :

System.Net.Http.HttpRequestException: An error occurred while sending the request.\n ---> System.IO.IOException: The response ended prematurely.\n   at System.Net.Http.HttpConnection.FillAsync()\n   at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)\n   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\n   --- End of inner exception stack trace ---\n

The way i'm matching the responses is either:

  1. .WithBody(new JsonMatcher(bodyContent)) if the request body is a JSON.
  2. .WithBody(new ExactMatcher(bodyContent)) if the request body contains encoded parameters.

The strange thing is that it doesn't reproduce every time.
Some CIs pass (and I can see the mock server debug of returning the proper response for the request), and some do not (in which I don't see any logs from the mock server for this request).

Please update me if you need more details.
I'm using WireMock.Net.StandAlone version 1.2.12 on .NET core.
The server is running inside a docker if that makes any difference.

Thanks

Originally created by @ilia-cy on GitHub (Aug 14, 2020). I'm using the mock server for some time now, and I recently started to encounter this strange issue. All mocked responses work properly, except for ones that are attempting to respond to POST requests that contain something in the body. These mocked responses fail our CI once in a while, as when the client performs the POST request it receives the following `HttpRequestException` : ``` System.Net.Http.HttpRequestException: An error occurred while sending the request.\n ---> System.IO.IOException: The response ended prematurely.\n at System.Net.Http.HttpConnection.FillAsync()\n at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)\n at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\n --- End of inner exception stack trace ---\n ``` The way i'm matching the responses is either: 1. `.WithBody(new JsonMatcher(bodyContent))` if the request body is a JSON. 2. `.WithBody(new ExactMatcher(bodyContent))` if the request body contains encoded parameters. The strange thing is that it doesn't reproduce every time. Some CIs pass (and I can see the mock server debug of returning the proper response for the request), and some do not (in which I don't see any logs from the mock server for this request). Please update me if you need more details. I'm using `WireMock.Net.StandAlone` version 1.2.12 on .NET core. The server is running inside a docker if that makes any difference. Thanks
adam added the question label 2025-12-29 08:25:31 +01:00
adam closed this issue 2025-12-29 08:25:31 +01:00
Author
Owner

@StefH commented on GitHub (Aug 14, 2020):

Is your application running on Linux or Windows?

And could it be that the HttpClient you are using is not used optimally? (there are several StackOverflow articles when you search : https://www.google.com/search?client=firefox-b-d&q=System.IO.IOException%3A+The+response+ended+prematurely.%5Cn+++at+System.Net.Http.HttpConnection.FillAsync%28%29%5C

@StefH commented on GitHub (Aug 14, 2020): Is your application running on Linux or Windows? And could it be that the HttpClient you are using is not used optimally? (there are several StackOverflow articles when you search : https://www.google.com/search?client=firefox-b-d&q=System.IO.IOException%3A+The+response+ended+prematurely.%5Cn+++at+System.Net.Http.HttpConnection.FillAsync%28%29%5C
Author
Owner

@StefH commented on GitHub (Aug 14, 2020):

And can you upgrade to latest version? Maybe your bug is related to https://github.com/WireMock-Net/WireMock.Net/issues/474 ?

@StefH commented on GitHub (Aug 14, 2020): And can you upgrade to latest version? Maybe your bug is related to https://github.com/WireMock-Net/WireMock.Net/issues/474 ?
Author
Owner

@ilia-cy commented on GitHub (Aug 14, 2020):

Running on Linux.
Nothing special regarding the HttpClient.
It is the standard .Net Core client, with no special tweaks or settings.

I'll try upgrading to the latest version to see whether it solves it.
Thanks!

@ilia-cy commented on GitHub (Aug 14, 2020): Running on Linux. Nothing special regarding the HttpClient. It is the standard .Net Core client, with no special tweaks or settings. I'll try upgrading to the latest version to see whether it solves it. Thanks!
Author
Owner

@ilia-cy commented on GitHub (Aug 20, 2020):

Upgrading the version to 1.2.18 solved the issue.
Thanks a lot!

@ilia-cy commented on GitHub (Aug 20, 2020): Upgrading the version to 1.2.18 solved the issue. Thanks a lot!
Author
Owner

@StefH commented on GitHub (Aug 20, 2020):

Hello @ilia-cy, I'm glad your issue is solved.

Closing this one now...

@StefH commented on GitHub (Aug 20, 2020): Hello @ilia-cy, I'm glad your issue is solved. Closing this one now...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#290