I have a standalone instance of Wiremock server. The mappings are stored as json files under the mappings folder. I have a POST request that needs to return a dynamic ID(integer) in the response. Is there a way to configure this in the json file?
4 Answers
To make the above examples work, I had to run the standalone jar with the --global-response-templating
. Then I saw, for example, {{now}}
working which is what I wanted. Not sure if the documentation specifies this -- I tried the always-useful --help.

- 6,827
- 3
- 20
- 43

- 1,513
- 4
- 18
- 34
In WireMock there are a number of response template helper functions for generating random strings. In the below example I'm using the one for generating a UUID, but several other options exist.
Mapping file: dynamic_id.json
{
"request": {
"method": "POST",
"url": "/dynamic_id"
},
"response": {
"headers": {
"Content-Type": "application/json"
},
"status": 200,
"body": "{{randomValue type='UUID'}}",
"transformers": ["response-template"]
}
}
Using an empty POST http://wiremock/dynamic_id will return an id similar to: c2e6bf32-c9a3-45c0-b988-94fad04cc7a2
.
Start WireMock:
java -jar wiremock-standalone-2.18.0.jar --port 8181 --verbose --local-response-templating

- 6,827
- 3
- 20
- 43
-
I tried this and I get no id back. The response to curl http://localhost:8080/dynamic_id -d foo=bar is {{randomValue type='UUID'}} -- not generating any random string. I am using the latest standalone WireMock jar. – Jeff Dec 03 '18 at 06:32
-
2Maybe you forgot to add `--local-response-templating` to wiremock... – MaxDhn Jan 29 '21 at 00:21
This seems like a perfect use-case for OpenTable's Wiremock Body Transformer.
It can be easily integrated with the Standalone Server like this:
java -cp "wiremock-body-transformer-1.1.6.jar:wiremock-2.3.1-standalone.jar" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --verbose --extensions com.opentable.extension.BodyTransformer
And allows you to easily specify a dynamic variable that you would want to match in the response.
Here is an example to get a random integer without having to specify anything in the request, however if you need to match a specific variable in the request to the response, then that is also very doable with this extension and numerous examples can be found in the readme.
{
"request": {
"method": "POST",
"urlPath": "/transform",
},
"response": {
"status": 200,
"body": "{\"randomInteger\": \"$(!RandomInteger)\"}",
"headers": {
"Content-Type": "application/json"
},
"transformers": ["body-transformer"]
}
}

- 2,785
- 2
- 16
- 31
-
I tried this: 1. Created a transform directory under mappings 2. Created a stub2.json file with the text indicated above. 3. WireMock failed until I removed the comma after transform 4. restarted WireMock and tried curling 5. Result was: {"randomInteger": "$(!RandomInteger)"}C02W31GSHTD8:transform -- not sure if the string is the random integer but it does not change. 6. I really want this to work not for random int but for date. Please let me know what I am missing and how to test this dynamic response using curl. – Jeff Dec 02 '18 at 23:57
-
Sorry, the string I included that starts with C02 is part of the command line. What I am seeing is the contents of the json file without an actual Random Integer, I think. – Jeff Dec 03 '18 at 06:28
-
@Jeff Can you should the code for how you are starting your Wiremock server? – Mark Han Dec 03 '18 at 16:57
-
1As indicated in my answer that I added after this question, I was able to get things to work by using the --global-response-templating switch. A. Koostra modified his answer along these lines. I can tell you that without this switch I tried many different things, of course without success. Maybe I missed this in the documentation but if it is in fact not present or not emphasized, it should be featured prominently in discussions of how to do dynamic responses in the documentation. I realize you provided a command line but dynamic responses can be done without the opentable stuff. – Jeff Dec 03 '18 at 18:50
As @Jeff mentions, If you are running it as a stand-alone process, you need to add this flag --global-response-templating
. This will apply templating to each and every reponse. However, few of your responses may be jsut plain json with no templating required.
In that case use --local-response-templating
. and add this field inside reponse json:
response:{
"transformers": ["response-template"]
}

- 2,175
- 1
- 21
- 24