Writing to the response body is invalid for responses with status code 204 #195

Closed
opened 2025-12-29 08:23:41 +01:00 by adam · 13 comments
Owner

Originally created by @gregoks on GitHub (Jul 29, 2019).

Hi,

Many times I see the following error when I use wiremock:

"HttpStatusCode set to 500 System.InvalidOperationException: Writing to the response body is invalid for responses with status code 204. at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ThrowWritingToResponseBodyNotSupported() at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.WriteAsync(ReadOnlyMemory`1 data, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) at WireMock.Owin.Mappers.OwinResponseMapper.MapAsync(ResponseMessage responseMessage, HttpResponse response) at WireMock.Owin.WireMockMiddleware.InvokeInternal(HttpContext ctx) at WireMock.Owin.GlobalExceptionMiddleware.InvokeInternal(HttpContext ctx)"

it comes from the following class:

WireMock.Owin.GlobalExceptionMiddleware+d__7.MoveNext

I thought that perhaps I'm trying to add mapping with response content for http status 204 but I havent found anything like that.

Do you have any idea about this sort of error?

Thanks,

Greg

Originally created by @gregoks on GitHub (Jul 29, 2019). Hi, Many times I see the following error when I use wiremock: "HttpStatusCode set to 500 System.InvalidOperationException: Writing to the response body is invalid for responses with status code 204. at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ThrowWritingToResponseBodyNotSupported() at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.WriteAsync(ReadOnlyMemory`1 data, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) at WireMock.Owin.Mappers.OwinResponseMapper.MapAsync(ResponseMessage responseMessage, HttpResponse response) at WireMock.Owin.WireMockMiddleware.InvokeInternal(HttpContext ctx) at WireMock.Owin.GlobalExceptionMiddleware.InvokeInternal(HttpContext ctx)" it comes from the following class: WireMock.Owin.GlobalExceptionMiddleware+<InvokeInternal>d__7.MoveNext I thought that perhaps I'm trying to add mapping with response content for http status 204 but I havent found anything like that. Do you have any idea about this sort of error? Thanks, Greg
adam added the bug label 2025-12-29 08:23:41 +01:00
adam closed this issue 2025-12-29 08:23:42 +01:00
Author
Owner

@StefH commented on GitHub (Jul 30, 2019):

If possible, please provide the mappings you use.

@StefH commented on GitHub (Jul 30, 2019): If possible, please provide the mappings you use.
Author
Owner

@gregoks commented on GitHub (Jul 31, 2019):

Thats the thing, I dont see that it's related to a mapping that I have added. is it possible?
I'll try to increase the log level to see if I can add more information.

@gregoks commented on GitHub (Jul 31, 2019): Thats the thing, I dont see that it's related to a mapping that I have added. is it possible? I'll try to increase the log level to see if I can add more information.
Author
Owner

@StefH commented on GitHub (Jul 31, 2019):

Or provide a demo project ?

@StefH commented on GitHub (Jul 31, 2019): Or provide a demo project ?
Author
Owner

@gregoks commented on GitHub (Aug 7, 2019):

@StefH just to update, I have tried to make my logs to be "Debug". still havent really figured out the issue, but I saw this error one time:

  • HttpStatusCode set to 500 Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Reading the request body timed out due to data arriving too slowly. See MinRequestBodyDataRate. at Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException.Throw(RequestRejectionReason reason) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1MessageBody.PumpAsync() at System.IO.Pipelines.PipeCompletion.ThrowLatchedException() at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result) at System.IO.Pipelines.Pipe.GetReadAsyncResult() at System.IO.Pipelines.Pipe.DefaultPipeReader.GetResult(Int16 token) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.CopyToAsync(Stream destination, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.CopyToAsyncInternal(Stream destination, CancellationToken cancellationToken) at WireMock.Util.BodyParser.ReadBytesAsync(Stream stream) at WireMock.Util.BodyParser.Parse(Stream stream, String contentType) at WireMock.Owin.Mappers.OwinRequestMapper.MapAsync(HttpRequest request) at WireMock.Owin.WireMockMiddleware.InvokeInternal(HttpContext ctx) at WireMock.Owin.GlobalExceptionMiddleware.InvokeInternal(HttpContext ctx)

and I would like to ask if you could create pre-release version with the following change in the WireMockMiddleware class:

In many cases I see the following error without any exception stack trace:

"HttpStatusCode set to 500"

because you error message without the exception in this line:

_options.Logger.Error("HttpStatusCode set to 500");

The error is very general, could add the exception itself to the error?
Or even if you could add the problematic mapping to the log message, that would be even better!

Thanks,

Greg

@gregoks commented on GitHub (Aug 7, 2019): @StefH just to update, I have tried to make my logs to be "Debug". still havent really figured out the issue, but I saw this error one time: - HttpStatusCode set to 500 Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Reading the request body timed out due to data arriving too slowly. See MinRequestBodyDataRate. at Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException.Throw(RequestRejectionReason reason) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1MessageBody.PumpAsync() at System.IO.Pipelines.PipeCompletion.ThrowLatchedException() at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result) at System.IO.Pipelines.Pipe.GetReadAsyncResult() at System.IO.Pipelines.Pipe.DefaultPipeReader.GetResult(Int16 token) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.CopyToAsync(Stream destination, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.CopyToAsyncInternal(Stream destination, CancellationToken cancellationToken) at WireMock.Util.BodyParser.ReadBytesAsync(Stream stream) at WireMock.Util.BodyParser.Parse(Stream stream, String contentType) at WireMock.Owin.Mappers.OwinRequestMapper.MapAsync(HttpRequest request) at WireMock.Owin.WireMockMiddleware.InvokeInternal(HttpContext ctx) at WireMock.Owin.GlobalExceptionMiddleware.InvokeInternal(HttpContext ctx) and I would like to ask if you could create pre-release version with the following change in the WireMockMiddleware class: In many cases I see the following error without any exception stack trace: "HttpStatusCode set to 500" because you error message without the exception in this line: _options.Logger.Error("HttpStatusCode set to 500"); The error is very general, could add the exception itself to the error? Or even if you could add the problematic mapping to the log message, that would be even better! Thanks, Greg
Author
Owner

@gregoks commented on GitHub (Aug 7, 2019):

Just to clarify, Im using wiremock as an mocking infra in a company I work for. Currently I have around 20 mocks set up on different ports and automation nightlies are running at the same time, adding miltiple mappings and sometimes the errors that Ive mentioned happen so without the logging, its hard to understand which mapping causes the error.

@gregoks commented on GitHub (Aug 7, 2019): Just to clarify, Im using wiremock as an mocking infra in a company I work for. Currently I have around 20 mocks set up on different ports and automation nightlies are running at the same time, adding miltiple mappings and sometimes the errors that Ive mentioned happen so without the logging, its hard to understand which mapping causes the error.
Author
Owner

@StefH commented on GitHub (Aug 7, 2019):

You mean this line?

83457c1601/src/WireMock.Net/Owin/WireMockMiddleware.cs (L132)

@StefH commented on GitHub (Aug 7, 2019): You mean this line? https://github.com/WireMock-Net/WireMock.Net/blob/83457c160152863e57104a4e9d9d62a11d20b57e/src/WireMock.Net/Owin/WireMockMiddleware.cs#L132
Author
Owner

@StefH commented on GitHub (Aug 7, 2019):

Can you install WireMock.Net.1.0.27-ci-11567.nupkg?

This version contains more logging. e423a995f4

@StefH commented on GitHub (Aug 7, 2019): Can you install `WireMock.Net.1.0.27-ci-11567.nupkg`? This version contains more logging. https://github.com/WireMock-Net/WireMock.Net/pull/309/commits/e423a995f4835fe261e8869ce04c95ea58969eee
Author
Owner

@gregoks commented on GitHub (Aug 8, 2019):

You mean this line?

83457c1601/src/WireMock.Net/Owin/WireMockMiddleware.cs (L132)

yes exactly. It would be great if you could add the exception to the error message or perhaps even the mapping that caused it..

@gregoks commented on GitHub (Aug 8, 2019): > You mean this line? > > https://github.com/WireMock-Net/WireMock.Net/blob/83457c160152863e57104a4e9d9d62a11d20b57e/src/WireMock.Net/Owin/WireMockMiddleware.cs#L132 yes exactly. It would be great if you could add the exception to the error message or perhaps even the mapping that caused it..
Author
Owner

@StefH commented on GitHub (Aug 8, 2019):

Can you install and try WireMock.Net.1.0.27-ci-11567.nupkg from MyGet?

@StefH commented on GitHub (Aug 8, 2019): Can you install and try WireMock.Net.1.0.27-ci-11567.nupkg from MyGet?
Author
Owner

@gregoks commented on GitHub (Aug 8, 2019):

Can you install WireMock.Net.1.0.27-ci-11567.nupkg?

This version contains more logging. e423a99

sure, ill try and let you know.

@gregoks commented on GitHub (Aug 8, 2019): > Can you install `WireMock.Net.1.0.27-ci-11567.nupkg`? > > This version contains more logging. [e423a99](https://github.com/WireMock-Net/WireMock.Net/commit/e423a995f4835fe261e8869ce04c95ea58969eee) sure, ill try and let you know.
Author
Owner

@gregoks commented on GitHub (Aug 8, 2019):

I have tried WireMock.Net.1.0.27-ci-11567 and the logs that you have added help! I saw the reason for the error, it was a mapping that led to service that took really long time to respond.

@gregoks commented on GitHub (Aug 8, 2019): I have tried WireMock.Net.1.0.27-ci-11567 and the logs that you have added help! I saw the reason for the error, it was a mapping that led to service that took really long time to respond.
Author
Owner

@StefH commented on GitHub (Aug 8, 2019):

Good to hear.

Also check if https://github.com/WireMock-Net/WireMock.Net/issues/308 is still happening, or maybe this is also fixed.

@StefH commented on GitHub (Aug 8, 2019): Good to hear. Also check if https://github.com/WireMock-Net/WireMock.Net/issues/308 is still happening, or maybe this is also fixed.
Author
Owner

@StefH commented on GitHub (Sep 23, 2019):

Closing

@StefH commented on GitHub (Sep 23, 2019): Closing
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#195