mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 14:20:29 +01:00
BodyParser Newtonsoft.Json.JsonReaderException #356
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.
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
Other related info
Provide additional information if any.
@StefH commented on GitHub (Aug 20, 2021):
Can you also post the file?
@Glen-Moonpig commented on GitHub (Aug 20, 2021):
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.Parseis doing?@StefH commented on GitHub (Aug 20, 2021):
In that case, can you post the complete request message you send?
@Glen-Moonpig commented on GitHub (Aug 20, 2021):
It is a GET request, there is no body to parse. The config for the request is
I expect
BodyParser.ShouldParseBodyis returning true when it should be returning false.@Glen-Moonpig commented on GitHub (Aug 20, 2021):
Should probably be
See here
@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?)
@Glen-Moonpig commented on GitHub (Aug 20, 2021):
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
@Glen-Moonpig commented on GitHub (Aug 20, 2021):
Here is the HttpRequestMessage that is sent
@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?
@Glen-Moonpig commented on GitHub (Aug 20, 2021):
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_ASPNETCOREpreprocessor 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...@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.
@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-urlencodedand the body is: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/tokenand I have this config on Wiremock:@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
and my content type header has charset included
Content-Type: application/x-www-form-urlencoded; charset=utf-8I can see that WiremockMiddleware has received the correct header
@Glen-Moonpig commented on GitHub (Aug 20, 2021):
Actually, it looks like
MediaTypeHeaderValue.TryParseshould 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):
...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.
@StefH commented on GitHub (Aug 20, 2021):
No worries.
I am glad your issue is solved.