Postman Tutorial
- 1. Introduction to Postman
- 2. Sending Requests & Body Types
- 3. Environments & Variable Scopes
- 4. Collections & Folders
- 5. Writing API Tests & Assertions
- 6. Automated Collection Runner
- 7. Postman CLI: Newman
- 8. Integration with CI/CD Pipelines
- 9. Mock Servers
- 10. API Monitoring & Uptime Alerting
- 11. Collaborative Workspaces
- 12. Generating API Documentation
- 13. Designing APIs (OpenAPI / Swagger)
- 14. Advanced Authorization
- 15. Postman Flows & Best Practices
9. Mock Servers
When building applications, frontend and backend development teams frequently block each other. The frontend team cannot build their interface because the backend database endpoints are still under development. Postman **Mock Servers** solve this fragmentation by allowing you to simulate a live backend API server.
What is a Mock Server?
A Mock Server is a cloud-based server hosted by Postman that simulates a real API backend. When you send a request to a mock server, Postman inspects the endpoint, matches it against a set of predefined **Examples (Mock Responses)**, and returns those static values instantly.
Step-by-Step: Creating a Mock Server
Step 1: Set Up the Collection & Requests
First, create a collection (e.g. `User API Mock`) and add a request that you want to simulate, such as:
GET https://api.example.com/users/profileStep 2: Add Examples (Mock Responses)
To tell the Mock Server what payload to return when a client hits this endpoint, you must create an **Example**:
- In the request tab, click the three dots next to the request name and select **Add Example** (or click **Examples** dropdown in the top right and click **Add Example**).
- In the Example request editor, you define the mock server's output:
- Set the Response Status Code (e.g., `200 OK`).
- Set the Response Headers (e.g., `Content-Type: application/json`).
- Write the Response Body payload (e.g.,
{"id": 99, "name": "Fake User", "status": "active"}).
- Click **Save** in the top right. You can add multiple examples for different codes, e.g., an example for `400 Bad Request` or `404 Not Found`!
Step 3: Spin Up the Mock Server
- Click the **Mock Servers** tab in the Postman left sidebar and click **Create Mock Server**.
- Select **Select an existing collection** and choose your mock collection.
- Provide a name for the mock server (e.g., `User Profiles Mock`).
- Click **Create Mock Server**. Postman will generate a unique, public cloud URL:
https://abcdefgh-1234-5678-90ab-cdef12345678.mock.pstmn.io
Step 4: Execute Against Mock Server
Copy the generated mock URL, paste it into your local browser, cURL console, or frontend React configurations:
GET https://abcdefgh-1234-5678-90ab-cdef12345678.mock.pstmn.io/users/profilePostman will resolve the endpoint, match your example, and return the fake JSON data instantly!
Advanced: Mock Server Matching Rules
How does Postman know which example to return if you have created multiple mock payloads for the same endpoint? Postman's routing algorithm matches:
- HTTP Request Method: If you send a `POST`, it will filter out `GET` examples.
- URL Path: If you hit `/users/profile`, it will not match `/users/settings`.
- Headers & Query Parameters: If you pass the custom header
x-mock-response-code: 404, Postman will bypass the standard `200 OK` example and return the matched `404 Not Found` payload!