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:
Method: GET
Request Path: hello
Response Code: 200
Response Body: Hello world
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?
Welcome to the Community and thank you for the question
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)
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.
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.
@danny-dainton 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.