</>

Technology

Postman

Difficulty

Advanced

Interview Question

What is a Mock Server in Postman and how do you use it?

Answer

Postman Mock Server

A Mock Server in Postman simulates API endpoints by returning predefined example responses. This allows frontend developers to work without a real backend, and testers to test without a live API.

Why Use Mock Servers?

  • Frontend development can proceed before backend APIs are ready
  • Test edge cases (slow responses, errors) without changing real API
  • Reduce dependency on backend teams
  • Test 3rd party API integrations without actually calling them

Creating a Mock Server

Method 1: From a Collection

  1. Select your Collection
  2. Click ...Mock Collection
  3. Give it a name (e.g., "User API Mock")
  4. Choose environment (optional)
  5. Click Create Mock Server
  6. Postman generates a URL like: https://abc123.mock.pstmn.io

Method 2: From Scratch

  1. Click Mock Servers in the sidebar
  2. Click + → Create Mock Server
  3. Add API paths and HTTP methods
  4. Define example responses

Adding Examples to Requests

  1. Open a saved request (e.g., GET /users)
  2. Send the real request to get a response
  3. Click Save ResponseSave as example
  4. The example is now returned by the mock server

Example Response Definition

For GET /api/users:

JSON
{
  "data": [
    { "id": 1, "name": "John", "email": "john@test.com" },
    { "id": 2, "name": "Jane", "email": "jane@test.com" }
  ],
  "total": 2
}

Using the Mock Server

Update your environment variable:

CODE
base_url = https://abc123.mock.pstmn.io

Send requests as normal — Postman returns mock responses.

Dynamic Mock Responses

Postman matches mock responses based on:

  1. URL path
  2. HTTP method
  3. Query parameters
  4. Request headers

Create multiple examples with different query params to return different mock data.

Follow AutomateQA

Related Topics