Hi Team, I have a query with respect to datetime ...
# help
p
Hi Team, I have a query with respect to datetime matching. My requirement is basically to check whether request has a json field start date which contains today's date or not. If yes I have to fetch a particular response. Here is my mapping file : { "name": "get available appointments", "request": { "urlPathPattern": "/getAppointments", "bodyPatterns":[{ "matchesJsonPath": { "expression": "$.startDate", "equalToDateTime" : "now", "truncateExpected" : "first hour of day" } }] }, Request data : { "locationId": "5481987", "startDate": "2023-05-05T060000+00:00", "endDate": "2023-06-01T060000+00:00", "userId": "Customer", "originatingSystemId": "13574" }
b
Hi @Preethu Kg, I think there are two ways to solve this. 1. using custom matching logic (see https://wiremock.org/docs/extending-wiremock/ under 'Custom Request Matchers)
p
Okay @Bas Dijkstra. But is there any other way to parse the incoming date and check whether its today's date
b
Yes, that was option 2. I was just writing you an example. Something like this should work, too (although I didn't test it yet)
Copy code
stubFor(post(urlEqualTo("/someUrl"))
                .withRequestBody(
                        matchingJsonPath("$.date", equalToDateTime(LocalDateTime.now()).actualFormat("yyyy/MM/dd"))
                )
                .willReturn(aResponse()
                        .withStatus(200))
        );
Just substitute your own JsonPath and datetime format
More examples at https://wiremock.org/docs/request-matching/ under 'Dates and times'. Examples in the docs are for headers but work for body elements, too.
p
Actually I had tried it with actualDateFromat as well. But it was not picking up
@Bas Dijkstra Adding one more query. For eg, I need to parse a date from my input request and use the same in response but different format Parameterization used in response json is as below : endDate :[ "{{date (parseDate jsonPath request.body '$.endDate') format ='yyyy-MM-dd'}}T190000.00Z" And I am getting below exception:
IMG_20230508_163322.jpg
b
That's a good one, I don't know, it's been a while since I've had to do that, and I don't really use JSON definitions a lot either