Examples vs Requests - adding mock responses to an existing collection

I’ve just completed the Bootcamp tutorial, Designing and mocking APIs > Design an API in a minute.

I’m confused about where I add mock responses to an existing collection.

These are the steps I followed:

I create a New > Mock Server. I’m provided with a table to create a collection of requests and responses:

Enter request paths for your API endpoints and then the expected response code and response body to create an example.

I create two items:

  1. Method: GET
    Request Path: hello
    Response Code: 200
    Response Body: Hello world
  2. Method: GET
    Request Path: goodbye
    Response Code: 200
    Response Body: Goodbye, cruel world

After clicking Next I name the mock server: Hello and Goodbye. Then I click Create Mock Server and finally Close the modal.

My Collection now correctly appears with two GET requests: hello and goodbye.

In the Environment dropdown at the top right of the UI, I choose Hello and Goodbye.

Now I can select one of the GET requests in my collection and Send it, and the response is as expected/mocked.

Now. When I click on the Examples dropdown, there is Default and Add Example. This is confusing as I added two examples when I created my mock server. But these two examples only appear in my (request) collection.

So I now want to add a third request to my collection. I click on the ··· menu and click Add Request. I only have access to the Request name here, so I name it hellohello and save it to my collection.

When I select the new request in the collection, all of the fields are blank. I set the method to GET and the URL to {{url}}/hellohello. But where do I set the mock response?

1 Like

Hey @dan1,

Welcome to the Community and thank you for the question :trophy:

I think I’m following you but correct me if this is not the case - when you created the Mock Server you created 1 example for each of the 2 different routes (/hello and /goodbye)

That would mean that each request in your Collection would have a single example each, rather than 2 examples on a single route.

The first Example has been named as Default but this can be changed to anything you what by clicking on the name in the Examples dropdown menu.

The Mock Creation flow is different from the single Add Request flow, as this can be done as a separate action and is not tied into being used solely for the Mock Servers.

Picking things up from your empty hellohello request, If you go to the examples section again and press the Add Example button it will open up the Example builder.

This will already be pre-populated with the {{url}}/hellohello request URL. For the most basic case, all you need to do is add a Response Body and a Response Code.

Once you press the Save Example button, that will now be ready to use in your actual request and return the details you provided.

In the main request view, you should now see the hellohello example in the dropdown.

By hitting the Send button, the request will be sent to the Mock Server and it will return the example request that you previously created.

I hope that makes sense and you’re able to follow the flow that I listed. Any questions or If you need me to clarify any of the behaviours, don’t hesitate to ask. :slight_smile:

1 Like

@dannydainton Many thanks for putting together all the screenshots and the explanation. Yes you are following me correctly.

I had a bit more of a play with it, and I now understand why each example is linked to a route, and where I went wrong.

The Mock Creation flow both creates the route and sets up the basic parameters of the (default) example.

Enter request paths for your API endpoints and then the expected response code and response body to create an example.

I’d read that as the whole table row being an example, not (A) a request path and (B) an example.

I now understand that there can be many examples for each route, because each has a plethora of customisation options and is not solely configured on the supplied route / response code / response body.

This is why the request path appears under the Collections sub-heading, but the example(s) appears under the Examples (n) dropdown for that request path. There can only be one of each request path, but depending on what you do or don’t send with the request, the response can be very different.

1 Like

Cool, was there anything else that you were unsure about?

Not at the moment thanks. I’ve just marked your answer as the solution :slight_smile:

1 Like