If I create a new example for a request backed by a Mock server, that new example appears in the examples list, but the Mock server continues to return the original example.
I donât see any way to select which example the mock server uses.
If I create a new example for a request backed by a Mock server, that new example appears in the examples list, but the Mock server continues to return the original example.
I donât see any way to select which example the mock server uses.
This largely depends on the matching algorithm.
https://learning.postman.com/docs/designing-and-developing-your-api/mocking-data/matching-algorithm/
The main idea is as follows: based on how your request looks like (HTTP method, address, query params, âŚ) the most appropriate response will be served back by the mock server.
Doesnât seem to work.
I have a simple request âgetâ with two example responses backed by a mock server.
One example has a request header of âAccept: text/plainâ and returns a plain text response body, the other example has a request header of âAccept: application/jsonâ and returns a JSON formatted body e.g. {ânameâ:âBobâ,âageâ:2}
If I configure the âgetâ request header to âAccept: text/plainâ it returns the JSON example using a response header âContent-Type: text/html; charset=utf-8â
If I add a new example with no headers specified at all that just returns the plain text ârubbishâ my âgetâ request now returns that instead, even after setting the request header to âAccept: application/jsonâ
The request example matching just doesnât seem to work.
Setting the x-mock-response-name header does work, but that seems like a primitive matching strategy.
The documentation lists the first element of the matching algorithm as âProperly formatted responsesâ, and when you edit the example there is even a section to enter the example request headers, but the matching algorthim seems to ignore the Accept header in selecting a response (which doesnât seem very good to start with), and it continues to ignore it even if you add the desired value into the exampleâs request headers. Which begs the question whatâs the point of being able to specify request headers in the examples if the matching algorithm is just going to ignore them anyway?
Hi @NeutrinoSunset,
You can select which headers the matching algorithm should respect by submitting a request header along with your request to the mock server. The header name is x-mock-match-request-headers
and the value is a comma-separated list of headers the mock server should match when selecting an example. (I think the header list needs to be lowercase at this point, but that seems like a bug.)
So, in your case, you would set the x-mock-match-request-headers
header to accept
. Make sure your examples include an Accept
request header, one with text/plain
and the other with application/json
. Now when you send a request to the mock server, you can control which example you get back by simply altering the Accept
header on the mock server request.
Hope that helps.
Best,
Kevin