There are different solutions for mocking an API endpoint. Such as using Microsoft Flow or doing it manually on a sever. But Postman mock servers can be a better fit if you already use Postman for your API development. Also, the initial use of their mock server is free with some limitations which I’m going to talk about. In this post I’m going through some questions such as, would we want to use postman mock server and how we can use it. I’m going to show you how you can return json and binary/octet-stream response from a mocked endpoint.
Why use Postman mock server
Sometimes we need to have the output of our APIs sooner than when it’s ready. Maybe because we work on the back end, but the front-end team are ahead of us and need the APIs faster to start working on their next phase of the project. In this situation, we can either release an unfinished version of our APIs, or we can create an endpoint that only return the output. In other word a mock version of the API. Also, some other situations where mocking APIs are useful are when we need those APIs as part of our end-to-end tests.
Limitation of Postman free account as it pertains to Mock Server
Postman mock server has some limitations for free accounts. Here’s a screenshot of those limitation for a free account.
Here’s another screenshot that shows the pricing for it if you need to go premium.
The plans might seem a little pricey, but that’s not only for mocking, but all other services that it provides. Also, for this specific need you might want to also look at Microsoft flow pricing that might be less than postman.
Creating a Mock Server
Here I describe shortly the steps necessary to create a mock server. There is already a good documentation about this so I wouldn’t go into much details here.
- Create a collection
- Hover on the collection and click on the arrow on the right side
- Navigate to mocks tab and click “create a mock server”
- Give your mock server a name and create the mock server, at this point you receive the URL to your mock server
- Now you need to create a request that will be mocked. To do that create a request in your collection, add a path using what you received as mock server URL, for example:
[mock-server-path]/api/users
Now you can add an example for your request that can be used as a mock.
- A simpler way is to use a preexisting request and save that as an example. First create a new request in your collection, make a request to receive a response, then click save as example
Now you can call the mocked API, here we send a request to the mocked API. In this case we previously saved the response from another request and now we’re using it for a mock request.
How can we use Postman Mock Server to mock APIs returning binary or application/octet-stream files?
Postman mock server does not support binary or application/octet-stream responses. At the time of this writing it only support/mock requests that return JSON or anything textual. Also, if we try to make a request to an endpoint that return binary and then save that as an example, it’s not going to work. What happen in this situation is that it’s going to corrupt the binary content of the response and what you receive from mock server is not going to be what was intended. But there’s a work around that can be used if we wanted to return binary or application/octet-stream responses that will work.
In order to do that you need to first create a request that intended to return binary or application/octet-stream response in your collection. Now try to add an example mock for that request. What we want to do for this request is to set a header for our response, so it redirects to the binary file we want to return as response. We can use the “Location” header to do that. Also, our response status code should return 303 redirect as can be seen from the image below.
Now we can save the example and after making a request to the mocked endpoint, we’re going to receive the binary file in the body of our response.
References and Further Readings
Get Started with Postman Mock Servers
Summary
In this post, I explained what Postman Mock server is and why do we want to use it. We also saw how to create a mock server and mock JSON response. Out of the box postman mock server does not support binary/stream output, so I introduced a work around for that also.