Open In App

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

Last Updated : 27 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

z214

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

z215

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

  • Request Method: This dropwown allows you to select our prefared http methods. example: GET, POST, PUT, DELETE
  • Request URL: This is used to specify the exact route in our mock endpoints.
  • Response Code: http response codes ranging from 100 to 599 can be used depending on our desired response
  • Response Body: This input field is used to pass a sample json response

z216

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.

Javascript




[{
    "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.

  • Environment: Environments help manage variables like URLs or tokens.
  • Make mock server private: is used when you want to make your mock api secure by adding authentication
  • Simulate fixed network delay: this feature for testing how your application behaves under different network conditions. It allows you to identify potential issues related to network latency and ensure that your application handles delays appropriately.

z217

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.

z218

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

z219



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads