Setting Up a Mock Server
To set up a mock server in Postman, we will need a collection because mocks in Postman are tied with a collection. Postman compares the requests and creates mock responses from the examples in the requests of a collection.
Before creating a Mock server in Postman, there are some important points about the mock server we must know:
- In the Postman app, the mock server is already integrated, so we don’t need to waste the time to download and install externally.
- The mock server supports CORS. CORS stands for Cross-Origin Resource Sharing. When using the mock server, it ensures you that you won’t get any cross-origin errors.
- The mock server is available in Postman, which is free to use.
In Postman, there are many ways to build a mock server:
- New Button
- Postman API
- Launch Screen
- Postman App
By default, the mocks are open to using. It is accessible publicly. When you make a private mock server, enterprise users and postman pro clients can share the underlying collection with their group and particular team members, or they can give permissions to edit or view.
Let’s create the mock server. Here we will discuss how to create a mock server with the New button.
- From the top left corner in the Header section of the Postman app, select the New button.
- Select the Mock Server.
- Once you select the Mock server, a new panel will be opened. Through this panel, we can create a new request.
In the above image, we have multiple options.
Here,
- The method is request type methods such as GET, POST, etc.
- Request Path is the URL of the request for your API.
- Response Code is the code in which we want to get in response.
- Response Body is the body of the response that you want to show in response.
Fill the column as given below:
- Click on Next. Now on the next tab, give the name of your mock server. You can give anything as per your choice.
In the above image, you can see one checkbox. If you want to make your mock server private, enable it.
Select the Create Mock Server button.
- The next panel is the review panel, which gives you the review that your mock server has been successfully built.
This screen will also display an URL through which we can access the mock server.
- Click on the Close button to close the panel.
Once you close the panel, a new collection has been generated with the given APIs.
- We can also see a new environment has been created with the same name.
Select this environment variable.
- Now, choose the first request from the collection and see the request url.
- Click on the send button. Here you can see the response.
Here, you can see, we got the same response body and the same response code as we have created while setting up the mock server.