Hello all! Iām trying to figure out how I can mock two different responses when the URL for both requests is identical.
Hereās an example: letās say Iāve got a GET URL which returns a list of cats. The list of cats returned will vary by who is asking, which is specified by the token that is passed in the header. So if I pass in a token for the admin, Iāll get all the cats, but if I pass in a token for a user, Iāll get just the userās cats.
How can I set up mocking for this situation, where the URL is identical, but the header passed in is different? Will the mock server recognize the header value? If so, can you provide me with an example? Thanks in advance!
Hi @conbrio96. Thanks for writing in
You can do that by using header matching in mock server. You can create two examples, one for user and one for admin and specify the headers that you want to use in them. When youāre calling the mock server url, you can use header matching by setting āx-mock-match-request-headersā = ātrueā in header of the mock request.
Detailed documentation can be found here: https://learning.getpostman.com/docs/postman/mock_servers/mock_with_api/#run-the-mock-service
Hi @vansh.singh- I gave this a try and Iām still stuck. The documentation says " To enable incoming mock request headers matching, you must ensure that x-mock-match-request-headers header is present in the request and its value is a comma separated string of header keys that you want to match against in the saved examples."
I gave my examples different Authorization values: one with āBearer Oneā and one with āBearer Twoā. Then I set up my mock request headers as shown in the attached image.
When I try to send the request, I get a message that a matched request cannot be found.
I also tried your original suggestion of setting x-mock-match-request-headers to true, and I can get a response that way, but itās always the example for Bearer Two, even if I have Bearer One in the header.
Can you show me an example where this is working, so I can model my mocks after that? Thank you!
Hi @conbrio96, could you try once with lower case āauthorizationā against āx-mock-match-request-headersā in mock request header and let us know if it works?