BodyParser Newtonsoft.Json.JsonReaderException #356

Closed
opened 2025-12-29 15:21:37 +01:00 by adam · 16 comments
Owner

Originally created by @Glen-Moonpig on GitHub (Aug 20, 2021).

Describe the bug

I keep getting a JsonReaderException when Wiremock is trying to parse a request body.
It works on my build server (linux dotnet image), not on my local machine (Windows 10, .NET Core 3.1).
I managed to get it working temporarily by deleting the bin folder and rebuilding, but that is no longer working.

image

image

I am using xUnit with Microsoft Visual Studio Professional 2019 Version 16.9.3

Expected behavior:

A clear and concise description of what you expected to happen.

Wiremock should read the incoming request and serve the JSON response.

Test to reproduce

The failure is on a GET request

                .Given(Request.Create()
                    .WithPath($"/SomeProjectKey/carts/{cartId}")
                    .UsingGet())
                .RespondWith(
                    Response.Create()
                        .WithStatusCode(200)
                        .WithBodyFromFile("./TestData/GetCart.json")
                );

Provide additional information if any.

Originally created by @Glen-Moonpig on GitHub (Aug 20, 2021). ### Describe the bug I keep getting a JsonReaderException when Wiremock is trying to parse a request body. It works on my build server (linux dotnet image), not on my local machine (Windows 10, .NET Core 3.1). I managed to get it working temporarily by deleting the bin folder and rebuilding, but that is no longer working. ![image](https://user-images.githubusercontent.com/45943914/130240305-588c7ea0-23ad-436b-bb07-029c2504c4c2.png) ![image](https://user-images.githubusercontent.com/45943914/130246365-b53de0c5-09aa-4cf3-9987-595d6a9e31a3.png) I am using xUnit with Microsoft Visual Studio Professional 2019 Version 16.9.3 ### Expected behavior: A clear and concise description of what you expected to happen. Wiremock should read the incoming request and serve the JSON response. ### Test to reproduce The failure is on a GET request ```csharp .Given(Request.Create() .WithPath($"/SomeProjectKey/carts/{cartId}") .UsingGet()) .RespondWith( Response.Create() .WithStatusCode(200) .WithBodyFromFile("./TestData/GetCart.json") ); ``` ### Other related info Provide additional information if any.
adam added the question label 2025-12-29 15:21:37 +01:00
adam closed this issue 2025-12-29 15:21:37 +01:00
Author
Owner

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

Can you also post the file?

@StefH commented on GitHub (Aug 20, 2021): Can you also post the file?
Author
Owner

@Glen-Moonpig commented on GitHub (Aug 20, 2021):

Can you also post the file?

Hello @StefH I have updated the issue description since I first created it. I think the exception is happening when Wiremock is trying to parse the body of the incoming request rather than the response body JSON. Is that what BodyParser.Parse is doing?

@Glen-Moonpig commented on GitHub (Aug 20, 2021): > Can you also post the file? Hello @StefH I have updated the issue description since I first created it. I think the exception is happening when Wiremock is trying to parse the body of the incoming request rather than the response body JSON. Is that what `BodyParser.Parse` is doing?
Author
Owner

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

In that case, can you post the complete request message you send?

@StefH commented on GitHub (Aug 20, 2021): In that case, can you post the complete request message you send?
Author
Owner

@Glen-Moonpig commented on GitHub (Aug 20, 2021):

In that case, can you post the complete request message you send?

It is a GET request, there is no body to parse. The config for the request is

.Given(Request.Create()
    .WithPath($"/SomeProjectKey/carts/{cartId}")
    .UsingGet())

I expect BodyParser.ShouldParseBody is returning true when it should be returning false.

@Glen-Moonpig commented on GitHub (Aug 20, 2021): > In that case, can you post the complete request message you send? It is a GET request, there is no body to parse. The config for the request is ```csharp .Given(Request.Create() .WithPath($"/SomeProjectKey/carts/{cartId}") .UsingGet()) ``` I expect `BodyParser.ShouldParseBody` is returning true when it should be returning false.
Author
Owner

@Glen-Moonpig commented on GitHub (Aug 20, 2021):

if (request.Body != null

Should probably be

if (string.IsNullOrWhitespace(request.Body)

See here

@Glen-Moonpig commented on GitHub (Aug 20, 2021): ```csharp if (request.Body != null ``` Should probably be ```csharp if (string.IsNullOrWhitespace(request.Body) ``` See [here](https://github.com/WireMock-Net/WireMock.Net/blob/dc078b57ea463e2c9c7da7f6579af34000596446/src/WireMock.Net/Owin/Mappers/OwinRequestMapper.cs#L57)
Author
Owner

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

Do you have only 1 mapping defined? That "GET" mapping?

And what kind of request do you send to WireMock.Net ?
(It looks like you send a POST request with an empty body, but with content-type json?)
So can you provide the request you send (maybe you send it via postman?)

@StefH commented on GitHub (Aug 20, 2021): Do you have only 1 mapping defined? That "GET" mapping? And what kind of request do you send to WireMock.Net ? (It looks like you send a POST request with an empty body, but with content-type json?) So can you provide the request you send (maybe you send it via postman?)
Author
Owner

@Glen-Moonpig commented on GitHub (Aug 20, 2021):

Do you have only 1 mapping defined? That "GET" mapping?

And what kind of request do you send to WireMock.Net ?
(It looks like you send a POST request with an empty body, but with content-type json?)
So can you provide the request you send (maybe you send it via postman?)

Only the one mapping for that path. I have other mapping on different paths.

The request is sent using commercetools-dotnet-core-sdk-v2.
It has HttpClient logging and I can see in the logs

Start processing HTTP request "GET" http://localhost:46871/SomeProjectKey/carts/Cart123
@Glen-Moonpig commented on GitHub (Aug 20, 2021): > Do you have only 1 mapping defined? That "GET" mapping? > > And what kind of request do you send to WireMock.Net ? > (It looks like you send a POST request with an empty body, but with content-type json?) > So can you provide the request you send (maybe you send it via postman?) Only the one mapping for that path. I have other mapping on different paths. The request is sent using [commercetools-dotnet-core-sdk-v2](https://github.com/commercetools/commercetools-dotnet-core-sdk-v2). It has HttpClient logging and I can see in the logs ``` Start processing HTTP request "GET" http://localhost:46871/SomeProjectKey/carts/Cart123 ```
Author
Owner

@Glen-Moonpig commented on GitHub (Aug 20, 2021):

Here is the HttpRequestMessage that is sent

image

@Glen-Moonpig commented on GitHub (Aug 20, 2021): Here is the HttpRequestMessage that is sent ![image](https://user-images.githubusercontent.com/45943914/130258957-64c09ac7-c6f9-4251-b8f9-6f99b4dc0cce.png)
Author
Owner

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

And WireMock is running at http://localhost:46871 ?

And what happens if you remove all other mappings, and just keep this 1 GET mapping?

@StefH commented on GitHub (Aug 20, 2021): And WireMock is running at http://localhost:46871 ? And what happens if you remove all other mappings, and just keep this 1 GET mapping?
Author
Owner

@Glen-Moonpig commented on GitHub (Aug 20, 2021):

And WireMock is running at http://localhost:46871 ?

And what happens if you remove all other mappings, and just keep this 1 GET mapping?

Yes that is the correct base URL for wiremock.
I have removed all other mappings and it still errors on that request.

The fact that this works on my linux build server but fails on my Windows machine makes me suspect that there is some difference in how .NET or Wiremock is behaving.
I'm wondering if it is to do with the USE_ASPNETCORE preprocessor directives in Wiremock, but that only seems to change when the target framework changes and target framework is the same. Both are running in Debug mode, it fails for me in Release too...

@Glen-Moonpig commented on GitHub (Aug 20, 2021): > And WireMock is running at http://localhost:46871 ? > > And what happens if you remove all other mappings, and just keep this 1 GET mapping? Yes that is the correct base URL for wiremock. I have removed all other mappings and it still errors on that request. The fact that this works on my linux build server but fails on my Windows machine makes me suspect that there is some difference in how .NET or Wiremock is behaving. I'm wondering if it is to do with the `USE_ASPNETCORE` preprocessor directives in Wiremock, but that only seems to change when the target framework changes and target framework is the same. Both are running in Debug mode, it fails for me in Release too...
Author
Owner

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

Interesting.
Are you able to create a console-app which uses that commercetools-dotnet-core-sdk-v2 to send the request ? Then I can debug WireMock.

@StefH commented on GitHub (Aug 20, 2021): Interesting. Are you able to create a console-app which uses that commercetools-dotnet-core-sdk-v2 to send the request ? Then I can debug WireMock.
Author
Owner

@Glen-Moonpig commented on GitHub (Aug 20, 2021):

I have managed to debug by Visual Studio decompiling the source. I can see that within the SDK I am using it is first sending a request to a token endpoint to fetch an auth token. The body it is sending is not JSON. The content type is application/x-www-form-urlencoded and the body is:

grant_type=client_credentials&scope=some_scope

image

Sorry to have wasted time talking about the GET request, but as far as I could see that is what was happening...

Are there any known issues with Wiremock handling application/x-www-form-urlencoded bodies?

The URL of the request is http://localhost:62306/oauth/token and I have this config on Wiremock:

MockServer
                .Given(Request.Create().WithPath("/oauth/token").UsingPost())
                .RespondWith(
                    Response.Create()
                        .WithStatusCode(200)
                        .WithBody(@"{ ""AccessToken"": ""test-mock-auth-token-value"" }")
                );
@Glen-Moonpig commented on GitHub (Aug 20, 2021): I have managed to debug by Visual Studio decompiling the source. I can see that within the SDK I am using it is first sending a request to a token endpoint to fetch an auth token. The body it is sending is not JSON. The content type is `application/x-www-form-urlencoded` and the body is: ``` grant_type=client_credentials&scope=some_scope ``` ![image](https://user-images.githubusercontent.com/45943914/130263041-0eeb09a1-d531-441e-a734-6237238d9347.png) Sorry to have wasted time talking about the GET request, but as far as I could see that is what was happening... Are there any known issues with Wiremock handling application/x-www-form-urlencoded bodies? The URL of the request is `http://localhost:62306/oauth/token` and I have this config on Wiremock: ```csharp MockServer .Given(Request.Create().WithPath("/oauth/token").UsingPost()) .RespondWith( Response.Create() .WithStatusCode(200) .WithBody(@"{ ""AccessToken"": ""test-mock-auth-token-value"" }") ); ```
Author
Owner

@Glen-Moonpig commented on GitHub (Aug 20, 2021):

I think the problem may be that the content type matcher does not include a wildcard at the end

new WildcardMatcher("application/x-www-form-urlencoded", true)

and my content type header has charset included

Content-Type: application/x-www-form-urlencoded; charset=utf-8

I can see that WiremockMiddleware has received the correct header

image

@Glen-Moonpig commented on GitHub (Aug 20, 2021): I think the problem may be that [the content type matcher](https://github.com/WireMock-Net/WireMock.Net/blob/dc078b57ea463e2c9c7da7f6579af34000596446/src/WireMock.Net/Util/BodyParser.cs#L58) does not include a wildcard at the end ```csharp new WildcardMatcher("application/x-www-form-urlencoded", true) ``` and my content type header has charset included `Content-Type: application/x-www-form-urlencoded; charset=utf-8` I can see that WiremockMiddleware has received the correct header ![image](https://user-images.githubusercontent.com/45943914/130265566-118db3ff-1bad-492d-8a3b-5692187c3ff1.png)
Author
Owner

@Glen-Moonpig commented on GitHub (Aug 20, 2021):

Actually, it looks like MediaTypeHeaderValue.TryParse should have extracted only the media type. I am not sure why it is using the JSON parser here.

@Glen-Moonpig commented on GitHub (Aug 20, 2021): Actually, it looks like `MediaTypeHeaderValue.TryParse` should have extracted only the media type. I am not sure why it is using the JSON parser here.
Author
Owner

@Glen-Moonpig commented on GitHub (Aug 20, 2021):

...I think this may have all been a waste of time. Wiremock attempts to deserialize as JSON and catch the JsonReaderException and ignore it. Because I am debugging and have CLR exceptions on it appeared to be a failure, but is actually expected.

Thanks for your time and patience.

@Glen-Moonpig commented on GitHub (Aug 20, 2021): ...I think this may have all been a waste of time. Wiremock attempts to deserialize as JSON and catch the JsonReaderException and ignore it. Because I am debugging and have CLR exceptions on it appeared to be a failure, but is actually expected. Thanks for your time and patience.
Author
Owner

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

No worries.
I am glad your issue is solved.

@StefH commented on GitHub (Aug 20, 2021): No worries. I am glad your issue is solved.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#356