Joana Baptista
03/14/2023, 2:52 PMHello everyone. I'm starting to use Wiremock and I have a question. I have a response with different id. How can I put the specific id in the url so that the response only returns the id part? For example:
URL:GET -> /contacts
and the answer:
{
"contacts": [
{
"id": "1",
"firstName": "Tom",
"lastName": "Smith",
"email": "<mailto:tom.smith@example.com|tom.smith@example.com>",
"dateAdded": "2021-01-03",
"companyId": "123"
},
{
"id": "2",
"firstName": "Suki",
"lastName": "Patel",
"email": "<mailto:spatel@example.com|spatel@example.com>",
"dateAdded": "2020-11-12",
"companyId": "123"
},
{
"id": "3",
"firstName": "Lexine",
"lastName": "Barnfield",
"email": "<mailto:barnfield8@example.com|barnfield8@example.com>",
"dateAdded": "2021-01-03",
"companyId": "234"
}
]
}
How can I create the request, if I put id=2, to just return the:
{
"id": "2",
"firstName": "Suki",
"lastName": "Patel",
"email": "<mailto:spatel@example.com|spatel@example.com>",
"dateAdded": "2020-11-12",
"companyId": "123"
}
I used the following form, but it gave error the body:matches JsonPath ($.id equal to 2), but not working. Thank you
Aaron
03/14/2023, 3:33 PMJoana Baptista
03/14/2023, 4:10 PMRob Elliot
03/15/2023, 9:34 AM/contacts/[0-9]+
(see "Regex matching on the path only").
Enable response templating.
Set the response body to be something like:
{
"id": "{{request.path.[1]}}",
"firstName": "Suki",
"lastName": "Patel",
"email": "<mailto:spatel@example.com|spatel@example.com>",
"dateAdded": "2020-11-12",
"companyId": "123"
}
Joana Baptista
03/15/2023, 7:20 PMRob Elliot
03/15/2023, 7:27 PMGET /contacts/1
to return the first element in the GET /contacts
response, and so on, you will need to add a specific stub for each contact with the appropriate path and body.
Generally it's a better idea not to, and to use random data or data from the request, but it will depend on your usecase.Joana Baptista
03/19/2023, 5:46 PM