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: