I am testing Postman Mock Server functionality and I am not able to understand how does the matching algorithm works.
Especially, is it possible to returns different response according to to matching request query params values ?
For instance:
http://domain/path&code=1 would return a save Response with the exactly same request (except the http/domain part which would be replaced by the mock cloud domain).
http://domain/path&code=2 would return another (different response) associated with the exact same request.
Hi @overflow! Our docs havenât been updated on this yet (working on it!) but we recently added two new optional headers: x-mock-response-name and x-mock-response-id, either of which can be used to specify the exact response you want to receive.
x-mock-response-name = The name of your saved example, which you can view in our UI or by calling our Postman API.
x-mock-response-id = The ID of your saved example, which you can find via exporting your collection or by calling our Postman API w/ GET (Single Collection), which will list the response ID under the request itâs associated with.
Thank you so much for your detailed answer ! So I understood how those headers works using Postman API however I donât think this might be of any use for my use case. Let me explain.
I am doing integration testing black boxing my application.
I send a batch of requests with associated responses.
I do send requests to routes exposed by my application which are POST with some parameters in the request body. My mock server is then called by the application but the Headers arenât passed from my entry request to the GET request onto the mock server.
Thatâs why my unique solution without making any modification to my app would have been examples served correctly by the matching algorithm.
Do you think thereâs a solution for this use case ?
As of today, there is no way for you to achieve what you want without specifying the extra headers.
The mock server currently does not look at the query params while matching requests to examples. This means if your examples differ in only the query params, mock will consistently return the first example that it finds that matches the path. You can of-course use the x-mock-response-name or the x-mock-response-id headers to explicitly specify which example you want Mock server to return.
You could maintain a mapping on your application side (mapping a signature of url + query params to the example id or name). You can also use a Mock server to dynamically serve this data and then add the corresponding x-mock-response-name to the request url that matches that signature.
I have a similar situation where I want to mock different responses for different query parameters on the same path.
Iâm a bit confused by the need to use the x-mock-* headers. I understand how the headers are used, and I can add the headers to have the correct response returned. However, unless Iâm missing something, the whole point of setting up the mock is so that I can âmockâ the actual API calls without having to change my calls. Having to add the headers means that Iâm having to modify my calls to include those headers.
My goal is to simply be able to configure the server that my application is pointing to for the API calls from the real (or yet-to-be-created) API server to the mock server. That means I should not have to modify my code to add headers that are specific to the mock service. Those headers have no bearing on the actual API, so I donât want to then remove those headers before I connect to the real API.
Am I missing something in how weâre supposed to go about using the mock servers?
We do plan to improve the Mock server functionality by providing better ways to match requests to responses and that includes query param matching. However, as of today, using the x-mock-* headers is the only workaround we have.
Any word on this? The custom header doesnât really work for integration testing or development - after all, we donât want to write production code that contains headers for Postman mocks⌠Weâre about to switching to an API gateway just for the sake of being able to develop against some somewhat dynamic endpoint mockups, but would love to keep things in Postman.
This is a huge disappointment for me. The documentation needs to be more clear on this. It can be very time consuming trying inject headers when you have multiple requests in a large system.
I agree this functionality is greatly needed and it would be best if we could also define POST body based responses - match a certain POST body.
Truly, we canât mock anything with the current system. Luckily, I have a very limited API for the next few weeks. After that, we will outgrow the current mock functionality and Postmanâs usefulness decreases rapidly.
I also fell into this trap. According to the documentation, the mock server matches strict request/response pairs so I assumed everything I enter in the âExample requestâ area is taken into account. It didnât work for my POST requests that only differ in the body. Before I was going to convert my requests to more familiar GET-based, I searched for the exact matching algorithm and stumbled upon this. I canât believe even this wonât work.
I might be understanding web APIs wrong, but isnât it normal that requests that differ by URL belong in different ârequestsâ and those with the same URL but with different parameters belong to the same request but to different Examples?
It didnât work for my POST requests that only differ in the body.
Same situation for me. Iâm using v6.7.2. I have two different examples in a POST request. They differ only in body x-www-form-urlencoded part. When I try to get, only the first one is returned from mock server in all conditions.
@abhinav Is this feature in postmanâs roadmap? If yes, in which version will it be released?
Currently, Iâll go with x-mock-response-name. I love Postman by the way. Great job!
This functionality is available in Postman mock server. You can create multiple examples with different query params that you want to match and mock server will give you responses accordingly.
For example, if you have two requests- response like the following:
{{url}}/user?name=abc
Response that you want for this is details of user with name abc
{{url}}/user?name=xyz
Response that you want for this is details of user with name xyz
You can create two different examples with different query params and different responses. When you call the mock server url with the respective query params, youâll get response.
Hope this adresses your concern. Please let us know if you need anything else.