```Hello everyone. I'm starting to use Wiremock an...
# general
j
Copy code
Hello 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
a
What’s different about the request where you want only the object where id=2 vs. the request where you want the entire contacts array?
j
I wanted that when filtering the request by a specific id, it would only return the json with part of that id and not its entire json. I tried to do it through parameters but it didn't work.
r
Create a new stub using path regex matching
/contacts/[0-9]+
(see "Regex matching on the path only"). Enable response templating. Set the response body to be something like:
Copy code
{
  "id": "{{request.path.[1]}}",
  "firstName": "Suki",
  "lastName": "Patel",
  "email": "<mailto:spatel@example.com|spatel@example.com>",
  "dateAdded": "2020-11-12",
  "companyId": "123"
}
j
Thank you, I understood better. But in this case the response is dynamic depending on the inputs that I put. Or the way you indicated, @Rob Elliot, works for cases where I don't want the response to be dynamic depending on the inputs, but when inserting id=2 in the url, for example, it will fetch only the json with id=2, ignoring id=1 and id=3?
r
Stubs are independent of each other; if you specifically need
GET /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.
j
Thank you so much for your help.