Request body JSON with added ID field as response

1,889 views
Skip to first unread message

Ruben Lassau-Strauven

unread,
Jul 28, 2021, 4:58:19 AM7/28/21
to wiremock-user
The API we're mocking has a POST endpoint to which a JSON object can be sent. It will return the newly created JSON object with the ID it was assigned. We would like to mock this behaviour.

Is it possible to use the request JSON body, add a field with a static value, and return that JSON as the response without custom transformers?

Request: 
{
  "firstName": "John",
  "lastName": "Doe"
}

Response
{
  "id": 1234
  "firstName": "John",
  "lastName": "Doe"
}

Thanks in advance!

thilmano_wiremockuser

unread,
Jul 28, 2021, 6:29:11 AM7/28/21
to wiremock-user

Hi,

Please review the response template features of Wiremock. That should help


Thanks

Ruben Lassau-Strauven

unread,
Jul 28, 2021, 8:17:20 AM7/28/21
to wiremock-user
Hello, thanks for your answer.

I have looked at the documentation on Response Templating, but I couldn't quite find what I need. I also looked at any open issues on the GitHub repository or entries on this mailing list, but to no avail.

Thanks.

Op woensdag 28 juli 2021 om 12:29:11 UTC+2 schreef thilmano_wiremockuser:

aaron...@willowtreeapps.com

unread,
Jul 28, 2021, 9:18:59 AM7/28/21
to wiremock-user
You can use the JSONPath Helper functionality to copy your values from your request body to the response body.
You can use the Random Value Helper to automatically assign a random ID value.
In order for these to work, you'll need to enable response templating.

Pseudo example that I tested and worked:
{
    "request": {
        "url": "/url",
        "method": "GET",
        "bodyPatterns": [{
                "equalToJson": {
                    "firstName": "${json-unit.any-string}",
                    "lastName": "${json-unit.any-string}"
                },
                "ignoreArrayOrder": true,
                "ignoreExtraElements": true
        }]
    },
    "response": {
        "status": 200,
        "body": "{ \"firstName\": {{jsonPath request.body '$.firstName'}}, \"lastName\": {{jsonPath request.body '$.lastName'}}, \"id\": {{randomValue type='UUID'}} }"
    }
}

You don't have to match on the bodyPatterns, but I find it to be helpful so that you don't run into an error where WireMock can't assign a value from the body.

Ruben Lassau-Strauven

unread,
Jul 28, 2021, 10:00:53 AM7/28/21
to wiremock-user
Thank you for the example.

That would indeed work as you propose, but we'd like to make this as generic as possible. 
Ideally, we don't have to manually set the JSON keys again (firstName, lastName) in the response body. That should all automatically be retrieved from the request body.
In other words: take whatever JSON body is sent in the request, add the ID field, and return that JSON.

Excuse me for not being specific enough in my initial question.

Thanks in advance!

Op woensdag 28 juli 2021 om 15:18:59 UTC+2 schreef aaron...@willowtreeapps.com:

aaron...@willowtreeapps.com

unread,
Jul 28, 2021, 12:00:30 PM7/28/21
to wiremock-user
You may be able to use the Parse Json Helper to assist, but I'm not super familiar with how it works. (I think you'll also need to be on 2.29.0 or higher).

Unfortunately, your best bet might be to create a custom transformer. Luckily, the transformer class exposes the request, so you can easily grab the request, add an id field, and then return that as the body.

Tiffany Marrel

unread,
Nov 24, 2022, 10:54:29 AM11/24/22
to wiremock-user
Although this query is 1 year old, it may still be relevant for more recent Wiremock users.
Using version 2.32, I managed to fulfill the requirement expressed above (return request body with id appended in a generic way) with the following response:

"response": {
    "transformers": ["response-template"],
    "status": 200,
     "headers": {
           "Content-Type": "application/json"
     },
     "bodyFileName": "responses/get_with_id_{{jsonPath request.body '$.name'}}.json"
}
and following response file content:
{
   {{#each (jsonPath request.body '$.') as |value key|}}
   "{{key}}": "{{value}}",
   {{/each}}
   "id": "my-id"
}

The extended expression of the each handlebar is only available with Handlebars 3.0+. This was already included in Wiremock 2.29.0.

nana peng

unread,
Jan 12, 2023, 10:05:40 PM1/12/23
to wiremock-user

I am trying to use the "bodyFileName" with .json file, and would like to use the handlebar expressions like #each in the .json file to render list, does this possibale?

Uri Maoz

unread,
Jan 13, 2023, 11:59:51 AM1/13/23
to wiremock-user
Hi Nana

Can you please post the question in our Slack community? We are moving the WireMock discussions to Slack community 

You can join here:


Thanks,

nana peng

unread,
Jan 14, 2023, 7:52:05 AM1/14/23
to wiremock-user
Sure, I will move to slack
Reply all
Reply to author
Forward
0 new messages