0
0
PostmanHow-ToBeginner ยท 4 min read

How to Create a Mock Server in Postman Quickly

To create a mock server in Postman, go to the workspace, click on New, then select Mock Server. Choose an existing collection or create a new one, define the environment, and Postman will generate a mock URL to simulate API responses.
๐Ÿ“

Syntax

Creating a mock server in Postman involves these steps:

  • New > Mock Server: Start creating a mock server.
  • Select Collection: Choose or create a collection with example requests and responses.
  • Environment: Optionally select an environment for variables.
  • Mock Server URL: Postman generates a URL to call the mock API.
text
1. Click 'New' button in Postman
2. Select 'Mock Server'
3. Choose or create a collection with example responses
4. Optionally select environment
5. Click 'Create Mock Server'
6. Use the generated mock URL to send requests
๐Ÿ’ป

Example

This example shows how to create a mock server for a simple GET request that returns a user profile.

First, create a collection with a GET request to /user/profile and add an example response with JSON data.

Then create the mock server using this collection. Use the mock URL to test the GET request and receive the example response.

json
{
  "info": {
    "name": "User Profile API",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "item": [
    {
      "name": "Get User Profile",
      "request": {
        "method": "GET",
        "url": {
          "raw": "/user/profile",
          "path": ["user", "profile"]
        }
      },
      "response": [
        {
          "name": "Example Response",
          "status": "200 OK",
          "code": 200,
          "body": "{\"id\": 1, \"name\": \"Alice\", \"email\": \"alice@example.com\"}",
          "header": [
            {"key": "Content-Type", "value": "application/json"}
          ]
        }
      ]
    }
  ]
}
Output
HTTP/1.1 200 OK Content-Type: application/json {"id": 1, "name": "Alice", "email": "alice@example.com"}
โš ๏ธ

Common Pitfalls

Common mistakes when creating mock servers in Postman include:

  • Not adding example responses to requests in the collection, so the mock server has no data to return.
  • Using incorrect HTTP methods or URLs that don't match the example requests.
  • Forgetting to select the correct environment if variables are used in the URL.
  • Expecting dynamic behavior; Postman mock servers return static example responses only.
text
Wrong way:
- Create a collection without example responses
- Create mock server from it
- Requests to mock URL return 404 or empty

Right way:
- Add example response to each request
- Create mock server from collection
- Requests return defined example data
๐Ÿ“Š

Quick Reference

StepDescription
1. New > Mock ServerStart creating a mock server in Postman
2. Select CollectionChoose or create a collection with example responses
3. Set EnvironmentOptionally select environment for variables
4. Create Mock ServerPostman generates a mock URL
5. Use Mock URLSend requests to mock API for testing
โœ…

Key Takeaways

Always add example responses to your collection requests before creating a mock server.
Use the generated mock server URL to simulate API calls without a real backend.
Mock servers in Postman return static example data, not dynamic responses.
Select the correct environment if your mock URLs use variables.
Check HTTP methods and paths match between your requests and example responses.