How to select which example is used by a mock server request?

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.

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.