Using Random Handlebar mixed with {{ response templating }} #520

Closed
opened 2025-12-29 08:29:26 +01:00 by adam · 3 comments
Owner

Originally created by @pouyaSobhanipour on GitHub (Jun 12, 2023).

Originally assigned to: @StefH on GitHub.

Hello

i am using Random HandleBar in .Net to receieve a randome response from a list of predefied values using wiremock.

String List: "{{Random Type=\"StringList\" Values=[\"a\", \"b\", \"c\"]}}"
in my case a, b or c are different json responces. as following :

a => {'result':'ERROR','description':'Value Not Found','ReqCode':'123'}
b => {'result':'ERROR','description':'Value set to Null','ReqCode':'456'}
c => {'result':'ERROR','description':'More than one item','ReqCode':'789'}

what i need to have in my project , is to understand which response ( a , b or c ) is associated with what request ( but i don't want the ReqCode to be fixed and pre-defiend).

we already know that one way to pass a dynamic value from request into response would be using "Response Templating - {{ }}" . but in this case , i could not find a way to pass this dynamic value using Random responce handlebar as the following :

"a\ => "{'result':'ERROR','description':'Value Not Found','ReqCode':'{{ my random Code }}'}
"b\ => "{'result':'ERROR','description':'Value set to Null','ReqCode':'{{my random Code }}'}
"c\ => "{'result':'ERROR','description':'More than one item','ReqCode':'{{my random Code }}'}

using this what i receive in response is the string itself ({{my random Code }}) rather than the value i passed in request body.
Is there any way to do this in wiremock ?

Thanks

Originally created by @pouyaSobhanipour on GitHub (Jun 12, 2023). Originally assigned to: @StefH on GitHub. Hello i am using **Random HandleBar** in .Net to receieve a randome response from a list of predefied values using wiremock. `String List: "{{Random Type=\"StringList\" Values=[\"a\", \"b\", \"c\"]}}" ` in my case a, b or c are different json responces. as following : a => {\'result\':\'ERROR\',\'description\':\'Value Not Found\',\'ReqCode\':\'123\'} b => {\'result\':\'ERROR\',\'description\':\'Value set to Null\',\'ReqCode\':\'456\'} c => {\'result\':\'ERROR\',\'description\':\'More than one item\',\'ReqCode\':\'789\'} what i need to have in my project , is to understand which response ( a , b or c ) is associated with what request ( but i don't want the ReqCode to be fixed and pre-defiend). we already know that one way to pass a dynamic value from request into response would be using "**Response Templating - {{ }}**" . but in this case , i could not find a way to pass this dynamic value using Random responce handlebar as the following : \"a\ => "{\'result\':\'ERROR\',\'description\':\'Value Not Found\',\'ReqCode\':\'{{ my random Code }}\'} \"b\ => "{\'result\':\'ERROR\',\'description\':\'Value set to Null\',\'ReqCode\':\'{{my random Code }}\'} \"c\ => "{\'result\':\'ERROR\',\'description\':\'More than one item\',\'ReqCode\':\'{{my random Code }}\'} using this what i receive in response is the string itself ({{my random Code }}) rather than the value i passed in request body. Is there any way to do this in wiremock ? Thanks
adam added the question label 2025-12-29 08:29:26 +01:00
adam closed this issue 2025-12-29 08:29:26 +01:00
Author
Owner

@StefH commented on GitHub (Jun 17, 2023):

So depending on a random value from a string list, you want to return a different message and a random code?

@StefH commented on GitHub (Jun 17, 2023): So depending on a random value from a string list, you want to return a different message and a random code?
Author
Owner

@pouyaSobhanipour commented on GitHub (Jun 19, 2023):

Hello .

I need to pass a value into the Random string list.
as an example

String List: "{{Random Type=\"StringList\" 
Values=[\"{'result':'ERROR','description':'Value Not Found','ReqCode':'{{ my random Code }}'}\",
              \"{'result':'ERROR','description':'Value set to Null','ReqCode':'{{my random Code }}'}\",
              \"{'result':'ERROR','description':'More than one item','ReqCode':'{{my random Code }}'}\"
]}}" 

so my request body would be (using a Post request) :

 {
    'ReqCode':'1234'
 }

and the response would be a random string, chosed from the Random List filed with the passed data.
so the response body comming from wiremock server would be:

{'result':'ERROR','description':'Value set to Null','ReqCode':'1234'}
How am i able to implement this ?
Thanks

@pouyaSobhanipour commented on GitHub (Jun 19, 2023): Hello . I need to pass a value into the Random string list. as an example ``` String List: "{{Random Type=\"StringList\" Values=[\"{'result':'ERROR','description':'Value Not Found','ReqCode':'{{ my random Code }}'}\", \"{'result':'ERROR','description':'Value set to Null','ReqCode':'{{my random Code }}'}\", \"{'result':'ERROR','description':'More than one item','ReqCode':'{{my random Code }}'}\" ]}}" ``` so my request body would be (using a Post request) : ``` { 'ReqCode':'1234' } ``` and the response would be a random string, chosed from the Random List filed with the passed data. so the response body comming from wiremock server would be: `{'result':'ERROR','description':'Value set to Null','ReqCode':'1234'} ` How am i able to implement this ? Thanks
Author
Owner

@StefH commented on GitHub (Jun 23, 2023):

See https://github.com/WireMock-Net/WireMock.Net/issues/956

@StefH commented on GitHub (Jun 23, 2023): See https://github.com/WireMock-Net/WireMock.Net/issues/956
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#520