Open In App

Create and use mock servers for API testing or development in Postman

Postman, a comprehensive API platform, includes a robust API client for exploring, testing and defining requests. Its notable feature is the ability to create mock servers, simulating API responses for testing and development purposes. Developers can organize requests in collections, generate mock servers, and customize settings to mimic desired API behavior. This functionality proves invaluable when testing against realistic responses in the absence of a fully developed or available API.

Prerequisites

What is a mock-server:

Mock server serves as a simulated or pseudo server that mimics authentic server behavior. They return controlled responses to requests, letting you design and create JSON API responses without writing a single line of code.



Let use assume that you are working as front-end developer in a large team that is tasked with building a web application that relies on a back-end API to fetch data. However, the back-end development is still in progress, and the API endpoints are not available yet. To avoid delays in front-end development, you decide to use a mock server in Postman.

Steps to Create and Use mock server using Postman API

Step 1: Open the postman application on your computer then click on the mock servers tab on the left side navigation bar.



Step 2: To create a new mock server, click on the create mock server or the plus symbol beside the collections tab.

Step 3: Create mock request:
To create a sample mock request, you’ll need to understand the following concept:

Now that you have understood the above concept, We are going to modify the input fields. for the purpose of this article, we will create a mock api to get the list of users in our application.

Start by setting the request url input field to users. Next copy JSON response below and paste in the response body. Then click next to continue.




[{
    "id" : 1,
    "name" : "Leanne Graham",
    "username" : "Bret",
    "email" : "Sincere@april.biz",
    "address" : {
        "street" : "Kulas Light",
        "suite" : "Apt. 556",
        "city" : "Gwenborough",
        "zipcode" : "92998-3874",
        "geo" : { "lat" : "-37.3159", "lng" : "81.1496" }
    },
    "phone" : "1-770-736-8031 x56442",
    "website" : "hildegard.org",
    "company" : {
        "name" : "Romaguera-Crona",
        "catchPhrase" :
            "Multi-layered client-server neural-net",
        "bs" : "harness real-time e-markets"
    }
},
{
    "id" : 2,
    "name" : "Ervin Howell",
    "username" : "Antonette",
    "email" : "Shanna@melissa.tv",
    "address" : {
        "street" : "Victor Plains",
        "suite" : "Suite 879",
        "city" : "Wisokyburgh",
        "zipcode" : "90566-7771",
        "geo" : { "lat" : "-43.9509", "lng" : "-34.4618" }
    },
    "phone" : "010-692-6593 x09125",
    "website" : "anastasia.net",
    "company" : {
        "name" : "Deckow-Crist",
        "catchPhrase" : "Proactive didactic contingency",
        "bs" : "synergize scalable supply-chains"
    }
},
{
    "id" : 3,
    "name" : "Clementine Bauch",
    "username" : "Samantha",
    "email" : "Nathan@yesenia.net",
    "address" : {
        "street" : "Douglas Extension",
        "suite" : "Suite 847",
        "city" : "McKenziehaven",
        "zipcode" : "59590-4157",
        "geo" : { "lat" : "-68.6102", "lng" : "-47.0653" }
    },
    "phone" : "1-463-123-4447",
    "website" : "ramiro.info",
    "company" : {
        "name" : "Romaguera-Jacobson",
        "catchPhrase" :
            "Face to face bifurcated interface",
        "bs" : "e-enable strategic applications"
    }
}
]

Step 4: Mock server configuration:

Within the configuration tab, we are going to set the mock server name to mock users api. You can set it to your preffared name. next, select the Save the mock server URL as an environment variable checkbox. now that you have completed the configuration, click on the create mock server button on your bottom right. before proceeding to the next step, let us understand some important concept in the configuration.

Step 5: Test mock request:

Navigate to the collections tab on the left side of the navigation bar, then click on the mock server api collection which we created and select users.

Next, click on the send button on the top right hand of your postman to see sample JSON request.


Article Tags :