Matching DateTime values in json body #696

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

Originally created by @DannyBoyIT on GitHub (Jun 2, 2025).

Originally assigned to: @StefH on GitHub.

I can't for the love of coding figure out how to successfully match specific requests that contain DateTime objects in the body.

Our setup:
We have a Web API that uses .NET 9. We have underlying dependencies to other Web APIs that I am trying to mock/fake with WireMock.

In my client code:

        //myDate is a DateTime with the correct time "2025-05-05"
        var request = new { SomeName = myName, SomeDate = myDate };
        //For debugging purposes
        var jsonString = JsonSerializer.Serialize(request, new JsonSerializerOptions { PropertyNamingPolicy =                 JsonNamingPolicy.CamelCase });
        var response = await HttpClient.PutAsJsonAsync(urlPath, request);

In my Custom Endpoint setup:

        var requestBody = new
        {
            SomeName  = "Test",
            SomeDate = DateTime.Parse("2025-05-05")
        };

        if (requestBody is not null)
            requestBuilder.WithBodyAsJson(requestBody);

        //For debugging purposes
        var jsonString = JsonSerializer.Serialize(requestBody, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });

        _server
            .Given(requestBuilder)
            .RespondWith(Response.Create()
                .WithBodyAsJson(responseBody!)
                .WithHeaders(new Dictionary<string, string>
                {
                    { "content-type", "application/json; charset=utf-8" }
                })
                .WithStatusCode(responseStatusCode));

The two different jsonString variables have identical values in both places. When my client makes the call though it gets back a 404 if I set the requestBody. If I do not set the requestBody I get an answer back from WireMock as expected.

What am I missing?

Originally created by @DannyBoyIT on GitHub (Jun 2, 2025). Originally assigned to: @StefH on GitHub. I can't for the love of coding figure out how to successfully match specific requests that contain DateTime objects in the body. Our setup: We have a Web API that uses .NET 9. We have underlying dependencies to other Web APIs that I am trying to mock/fake with WireMock. In my client code: ``` c# //myDate is a DateTime with the correct time "2025-05-05" var request = new { SomeName = myName, SomeDate = myDate }; //For debugging purposes var jsonString = JsonSerializer.Serialize(request, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); var response = await HttpClient.PutAsJsonAsync(urlPath, request); ``` In my Custom Endpoint setup: ``` c# var requestBody = new { SomeName = "Test", SomeDate = DateTime.Parse("2025-05-05") }; if (requestBody is not null) requestBuilder.WithBodyAsJson(requestBody); //For debugging purposes var jsonString = JsonSerializer.Serialize(requestBody, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); _server .Given(requestBuilder) .RespondWith(Response.Create() .WithBodyAsJson(responseBody!) .WithHeaders(new Dictionary<string, string> { { "content-type", "application/json; charset=utf-8" } }) .WithStatusCode(responseStatusCode)); ``` The two different `jsonString` variables have identical values in both places. When my client makes the call though it gets back a `404` if I set the `requestBody`. If I do not set the `requestBody` I get an answer back from WireMock as expected. What am I missing?
adam added the question label 2025-12-29 15:31:10 +01:00
adam closed this issue 2025-12-29 15:31:10 +01:00
Author
Owner

@StefH commented on GitHub (Jun 2, 2025):

@DannyBoyIT
Did you check this page?
https://github.com/wiremock/WireMock.Net/wiki/Request-Matching-Tips

@StefH commented on GitHub (Jun 2, 2025): @DannyBoyIT Did you check this page? https://github.com/wiremock/WireMock.Net/wiki/Request-Matching-Tips
Author
Owner

@DannyBoyIT commented on GitHub (Jun 3, 2025):

No, I didn't see that before but when I try to execute the request to http://localhost:62137/__admin/requests (the WireMock instance while running my integration tests) I get a 404 with Body "Status": "No matching mapping found"

@DannyBoyIT commented on GitHub (Jun 3, 2025): No, I didn't see that before but when I try to execute the request to `http://localhost:62137/__admin/requests` (the WireMock instance while running my integration tests) I get a 404 with Body `"Status": "No matching mapping found"`
Author
Owner

@StefH commented on GitHub (Jun 3, 2025):

In order to access the __admin, you need to start the WireMock.Net server with the admin interface enabled.
https://github.com/wiremock/WireMock.Net/wiki/Admin-API-Reference#admin-api-reference

or

https://github.com/wiremock/WireMock.Net/blob/master/src/WireMock.Net.Minimal/Settings/WireMockServerSettings.cs#L59

@StefH commented on GitHub (Jun 3, 2025): In order to access the `__admin`, you need to start the WireMock.Net server with the admin interface enabled. https://github.com/wiremock/WireMock.Net/wiki/Admin-API-Reference#admin-api-reference or https://github.com/wiremock/WireMock.Net/blob/master/src/WireMock.Net.Minimal/Settings/WireMockServerSettings.cs#L59
Author
Owner

@StefH commented on GitHub (Jun 4, 2025):

@DannyBoyIT
Note that the matching details is also visible in the logging (I thjnk).

@StefH commented on GitHub (Jun 4, 2025): @DannyBoyIT Note that the matching details is also visible in the logging (I thjnk).
Author
Owner

@DannyBoyIT commented on GitHub (Jun 10, 2025):

I got it to work by using a .ToString() with CultureInfo.CurrentCulture on the expected DateTime.
It seems that the matching criterion for dates is much stricter than the matching dotnet does for the endpoint parameters when passing a DateTime parameter both as a body and as a query parameter. The dotnet endpoint seem to understand the given date without any culture conversion needed.

@DannyBoyIT commented on GitHub (Jun 10, 2025): I got it to work by using a `.ToString()` with `CultureInfo.CurrentCulture` on the expected `DateTime`. It seems that the matching criterion for dates is much stricter than the matching dotnet does for the endpoint parameters when passing a `DateTime` parameter both as a body and as a query parameter. The dotnet endpoint seem to understand the given date without any culture conversion needed.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#696