Mocked responses where the URL is identical

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 :slight_smile:
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

Please let us know if you need more info

3 Likes

Thanks so much, @vansh.singh! Iā€™ll give this a try tomorrow! :slight_smile:

1 Like

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. Mock%20headers

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?

1 Like

Yes, @vansh.singh, that did it! Itā€™s working now. Thank you! :slight_smile:

1 Like

This just helped me as well. Itā€™s not intuitive that the list of headers needs to be lowercase.

Suggestions: