Hi , I am trying to use the response templating to...
# help
r
Hi , I am trying to use the response templating to play with dates. https://wiremock.org/docs/response-templating/#date-and-time-helpers If put this as in the template
Copy code
"body: "{{now}}"
it gives the the desired result However if I try to use the same in bodyFile and put the {{now}} in the content of the file, it does not seem to have any effect. Is the syntax to be used different when we put the template in a file using "bodyFile" VS when we inline the template using "body"
The following works fine on both inline as well as bodyFile
Copy code
{{now offset='1 years'}}
while {{now}} does not work. I am surely missing something.
t
That’s strange. Let me see if I can replicate.
Are you on the latest WireMock version?
r
Thanks for the help. I am on 3.2.0 standalone jar
t
Did you include
"transformers": ["response-template"]
on your stub?
Works for me with this
r
Great to hear that it works. Yes I did have the transformers
Copy code
"response": {
        "status": 200,
        
        
       "bodyFileName" : "1.xml",
        
    
        
        "transformers": [
          "response-template"
        ],
        "headers": {
          "Content-Type": "application/xml"
        }
      }
my template file is
Copy code
<RealTimePrice>
                        <period>125</period>
                        <reportType>REP</reportType>
                        <tradingDate>02-Aug-2023</tradingDate>
                        
                        <A>{{now offset='1 years'}} </A>
                        <B>{{now}}</B>
                        <demand>6835.977</demand>
                        <tcl>0.000</tcl>
                        <USEP>84.99</USEP>
                        <lcp>0.00</lcp>
                        <regulation>.01</regulation>
                        <primaryReserve>.01</primaryReserve>
                        <secondaryReserve/>
                        <contingencyReserve>.1</contingencyReserve>
                        <eheur>0.50</eheur>
                        <solar>282.53</solar>
            </RealTimePrice>
output is
Copy code
<RealTimePrice>
    <period>125</period>
    <reportType>REP</reportType>
    <tradingDate>02-Aug-2023</tradingDate>
    <A>2024-10-05T16:17:52Z </A>
    <B></B>
    <demand>6835.977</demand>
    <tcl>0.000</tcl>
    <USEP>84.99</USEP>
    <lcp>0.00</lcp>
    <regulation>.01</regulation>
    <primaryReserve>.01</primaryReserve>
    <secondaryReserve/>
    <contingencyReserve>.1</contingencyReserve>
    <eheur>0.50</eheur>
    <solar>282.53</solar>
</RealTimePrice>
l
Very strange. Just tried myself with your payloads and it seems to work for me. The response I get is:
Copy code
<RealTimePrice>
    <period>125</period>
    <reportType>REP</reportType>
    <tradingDate>02-Aug-2023</tradingDate>
    <A>2024-10-05T16:28:26Z</A>
    <B>2023-10-05T16:28:26Z</B>
    <demand>6835.977</demand>
    <tcl>0.000</tcl>
    <USEP>84.99</USEP>
    <lcp>0.00</lcp>
    <regulation>.01</regulation>
    <primaryReserve>.01</primaryReserve>
    <secondaryReserve/>
    <contingencyReserve>.1</contingencyReserve>
    <eheur>0.50</eheur>
    <solar>282.53</solar>
</RealTimePrice>
r
hmmm...let me think harder - what am i doing wrong....any wild guess
I think I have some clue - let me share that
Inside a loop is this the right way to do it.... It works when I don't have the looping, but with loop it does not.
Copy code
{{#each (range 1 3) as |index|}}

			
			
			<RealTimePrice>
                        <period>125</period>
                        <reportType>REP</reportType>
                        <tradingDate>02-Aug-2023</tradingDate>
                        
                        <A>{{now offset='1 years'}} </A>
                        <B>{{now}}</B>
                        <demand>6835.977</demand>
                        <tcl>0.000</tcl>
                        <USEP>84.99</USEP>
                        <lcp>0.00</lcp>
                        <regulation>.01</regulation>
                        <primaryReserve>.01</primaryReserve>
                        <secondaryReserve/>
                        <contingencyReserve>.1</contingencyReserve>
                        <eheur>0.50</eheur>
                        <solar>282.53</solar>
            </RealTimePrice>

			{{/each}}
t
I have a theory as to why this is happening, and I think it’s just a quirk of Handlebars
When you’re inside a nested scope like the one that’s created inside
{{#each}}
it’s trying to look up
now
as if it’s a piece of data.
l
I guess you could use this as a workaround -
<B>{{now offset='0 years'}}</B>
r
Yeahh
t
I just tried that workaround and it seems to work
r
Sure. Again thanks a lot.
May I ask if it is ok to have complex looping etc or that is too much going outside the limits. also is it possible to increment dates etc using math functions. What is the suggested way to debug , when things are stuck....shd we look at handle bars documentation ?
t
Complex looping should be fine, although as with programming generally there are limits.
r
Got it. templates seem to be very powerful. https://wiremock.org/docs/response-templating/ - if there is a table of content for sub heading for this page - it would be very helpful.
t
You should be able to use the
math
helper with dates, given a bit os wrangling
r
ok
t
We’re about to restructure the docs
👏 1
The Cloud docs have mostly the same info and are a bit better structured at the moment: https://docs.wiremock.io/response-templating/basics/
r
Got it - thanks for guiding on that