[Question] Dates in response templates #330

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

Originally created by @Narcalen on GitHub (Jan 25, 2021).

Is it possible to add formatted dates to response templates? e.g. I would like to do smth like this:

"Response": {
    "StatusCode": 200,
    "Headers": { "Content-Type": "application/json; charset=utf-8" },
    "BodyAsJson": {
      "data": {
            "id": "{{Random Type=\"Integer\" Min=100 Max=999999}}",
            "status": "draft",
            "createdAt": "{{now format='yyyy-MM-dd'}}",
            "updatedAt": "{{now}}",
      }
    },
    "UseTransformer": true
  }

Where now is current datetime, not data extracted from request

Originally created by @Narcalen on GitHub (Jan 25, 2021). Is it possible to add formatted dates to response templates? e.g. I would like to do smth like this: ``` json "Response": { "StatusCode": 200, "Headers": { "Content-Type": "application/json; charset=utf-8" }, "BodyAsJson": { "data": { "id": "{{Random Type=\"Integer\" Min=100 Max=999999}}", "status": "draft", "createdAt": "{{now format='yyyy-MM-dd'}}", "updatedAt": "{{now}}", } }, "UseTransformer": true } ``` Where now is current datetime, not data extracted from request
adam added the feature label 2025-12-29 15:21:01 +01:00
adam closed this issue 2025-12-29 15:21:01 +01:00
Author
Owner

@StefH commented on GitHub (Jan 25, 2021):

Hello @Narcalen

This is not yet supported in WireMock.Net but this could be added to Handlebars.Net.Helpers

I will take a look if it can be added there.

@StefH commented on GitHub (Jan 25, 2021): Hello @Narcalen This is not yet supported in WireMock.Net but this could be added to Handlebars.Net.Helpers I will take a look if it can be added there.
Author
Owner

@StefH commented on GitHub (Jan 26, 2021):

@Narcalen

I'm looking at the code from Handlebars.Net/Helpers and in this case, the string to use would be like:

"{{Format (Now) \"yyyy-MM-dd\"}}"

Would be work for you?

@StefH commented on GitHub (Jan 26, 2021): @Narcalen I'm looking at the code from Handlebars.Net/Helpers and in this case, the string to use would be like: ``` c# "{{Format (Now) \"yyyy-MM-dd\"}}" ``` Would be work for you?
Author
Owner

@StefH commented on GitHub (Jan 27, 2021):

Or another possible solution will be:

"{{Now \"yyyy-MM-dd\"}}"
@StefH commented on GitHub (Jan 27, 2021): Or another possible solution will be: ``` json "{{Now \"yyyy-MM-dd\"}}" ```
Author
Owner

@Narcalen commented on GitHub (Jan 27, 2021):

Thank you for the response! I have tried the second option and I am getting the following response: Template references a helper that is not registered. Could not find helper 'Now'

Is it possible to register additional handlebar helpers?

@Narcalen commented on GitHub (Jan 27, 2021): Thank you for the response! I have tried the second option and I am getting the following response: Template references a helper that is not registered. Could not find helper 'Now' Is it possible to register additional handlebar helpers?
Author
Owner

@StefH commented on GitHub (Jan 27, 2021):

Sorry for the confusion I caused.

This new functionality is now present in Handlebars.Net.Helpers however not yet used by WireMock.Net

I'm working in that and I keep you informed.

@StefH commented on GitHub (Jan 27, 2021): Sorry for the confusion I caused. This new functionality is now present in Handlebars.Net.Helpers however not yet used by WireMock.Net I'm working in that and I keep you informed.
Author
Owner

@StefH commented on GitHub (Jan 30, 2021):

@Narcalen
I've added functionality to Handlebars.Net.Helpers and these are used in WireMock.Net

Please try preview version WireMock.Net.1.4.3-ci-14593 from MyGet.

@StefH commented on GitHub (Jan 30, 2021): @Narcalen I've added functionality to Handlebars.Net.Helpers and these are used in WireMock.Net Please try preview version `WireMock.Net.1.4.3-ci-14593` from MyGet.
Author
Owner

@Narcalen commented on GitHub (Feb 3, 2021):

@StefH
Thank you for the update
I have updated to WireMock.Net.1.4.3-ci-14593 and tried the following options:
"{{Format (Now) \"yyyy-MM-dd\"}}"
"{{Now \"yyyy-MM-dd\"}}"

For both options, I received the following error:
{ "Status": "Template references a helper that cannot be resolved. Helper 'Format'" }

Moreover, I have received the same errors for other helpers, e.g.:
{ "Status": "Template references a helper that cannot be resolved. Helper 'Random'" }

Could you please suggest if I am missing anything?

@Narcalen commented on GitHub (Feb 3, 2021): @StefH Thank you for the update I have updated to WireMock.Net.1.4.3-ci-14593 and tried the following options: `"{{Format (Now) \"yyyy-MM-dd\"}}"` `"{{Now \"yyyy-MM-dd\"}}"` For both options, I received the following error: `{ "Status": "Template references a helper that cannot be resolved. Helper 'Format'" }` Moreover, I have received the same errors for other helpers, e.g.: `{ "Status": "Template references a helper that cannot be resolved. Helper 'Random'" }` Could you please suggest if I am missing anything?
Author
Owner

@StefH commented on GitHub (Feb 4, 2021):

Can you share your example project?

@StefH commented on GitHub (Feb 4, 2021): Can you share your example project?
Author
Owner

@Narcalen commented on GitHub (Feb 8, 2021):

https://github.com/Narcalen/example-wiremock-net

This mapping works fine with version 1.4.3 and returns Template references a helper that cannot be resolved error with 1.4.3-ci-14626

@Narcalen commented on GitHub (Feb 8, 2021): https://github.com/Narcalen/example-wiremock-net This mapping works fine with version 1.4.3 and returns `Template references a helper that cannot be resolved` error with 1.4.3-ci-14626
Author
Owner

@StefH commented on GitHub (Feb 8, 2021):

This should work:

"{{Random Type=\"Integer\" Min=100 Max=999999}} {{DateTime.Now}} {{DateTime.Now \"yyyy-MMM\"}} {{String.Format (DateTime.Now) \"MMM-dd\"}}"

Use exact version 1.4.3-ci-14635

@StefH commented on GitHub (Feb 8, 2021): This should work: ``` c# "{{Random Type=\"Integer\" Min=100 Max=999999}} {{DateTime.Now}} {{DateTime.Now \"yyyy-MMM\"}} {{String.Format (DateTime.Now) \"MMM-dd\"}}" ``` Use exact version 1.4.3-ci-14635
Author
Owner

@Narcalen commented on GitHub (Feb 9, 2021):

@StefH this version works fine for me. Thanks!

@Narcalen commented on GitHub (Feb 9, 2021): @StefH this version works fine for me. Thanks!
Author
Owner

@StefH commented on GitHub (Feb 9, 2021):

great!

You can expect a new 1.4.4 in some time

@StefH commented on GitHub (Feb 9, 2021): great! You can expect a new 1.4.4 in some time
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#330