Make calls to mock servers

View as Markdown

After setting up a mock server, you can send requests to it and inspect its activity. Postman supports two types of mock servers: those deployed from a mock and those created from a collection. How you call them and what you can inspect differs between the two.

Call a mock server deployed from a mock

A mock server deployed from a mock runs your custom JavaScript request handlers in the Postman cloud. Before sending requests to a deployed mock server, you can also run the mock locally to test during development. Once deployed, you can send requests to the mock server URL and inspect its activity.

Send local requests to a mock

In Local View, you can send local requests to a mock using HTTP requests. Then you can observe the responses to validate that your mock behaves as expected. The URL is http://localhost:<port> where the port number is specified in your mock configuration. You can manually start and stop the mock to control when it listens for requests.

Sending local requests to a mock requires the Postman desktop app.

In Cloud View, you can also deploy a mock as a mock server.

  1. Click the Items icon Items tab in the sidebar.
  2. Click Mocks.
  3. Select the mock.
  4. Click Start in the upper right. You can also select Options icon More actions > Run locally next to a mock in the sidebar.
  5. In the upper right, click Open in Postman icon Open in a new request to open a new request with the URL auto-filled. You can also click Copy icon Copy URL to copy the URL and use it in an HTTP request.
  6. Send requests to the URL and observe the responses, simulating a real API.

Click Stop in the upper right to stop the mock.

Send requests to a deployed mock server

Deploying a mock as a mock server is supported in Cloud View for Solo, Team, and Enterprise plans only. For more information, see the pricing page.

After you deploy a mock as a mock server, you can send requests to the mock server URL using an HTTP request in Cloud View. Then you can observe the responses to validate that your mock server behaves as expected. Mock servers listen for incoming requests and return responses based on the logic you defined in the implementation.

  1. Click the Services icon Services tab in the sidebar.

  2. Click Mock Servers.

  3. Select the mock server. You can identify a mock server based on a mock by the code icon Code icon.

  4. In the upper right, click Open in Postman icon Open in a new request to open a new request with the URL auto-filled. You can also click Copy icon Copy URL to copy the URL and use it in an HTTP request.

  5. Send requests to the URL and observe the responses, simulating a real API.

    • If the mock server is private, you must either have access to the workspace or include a valid x-api-key header with a Postman API key.
    • If the mock server is public, anyone with the mock server URL can send requests to it.

In the upper right, you can click Code icon View Mock to view the mock implementation.

Inspect mock activity

View requests, responses, and state information for your mock. You can inspect a local mock in Local View and Cloud View. If you’ve deployed a mock as a mock server, you can also inspect the mock server in Cloud View.

  1. Choose one of the following:

    • Inspect a local mock — Click the Items icon Items tab in the sidebar, click Mocks, then select a mock.
    • Inspect a deployed mock server — Click the Services icon Services tab in the sidebar, click Mock Servers, then select a mock server.
  2. Use the Session dropdown list to inspect a specific state session. Requests that use the same session identifier share state and display together.

  3. Use the Logs and State tabs to inspect requests, responses, and state information for the mock.

View logs

View incoming requests, responses, and state operations in the Logs tab.

You can filter logs by path, type, method, status, time range, and header to focus on specific requests and responses. For example, you can filter by a specific endpoint to see all requests made to that endpoint and the corresponding responses.

Select a log entry to view more details about the request and response, including headers and body content.

Logs with the simulator icon Stroke icon indicate requests that ran with a simulation applied.

Click Delete icon Clear logs to remove all logs for the mock.

View state

If your mock uses pm.state, you can inspect the current state values for the selected session in the State tab.

State entries are displayed as key-value pairs. Expand a state entry to view its contents and inspect how requests have modified state over time.

Click Delete icon Clear session to remove all state values for the selected session.

Call a mock server deployed from a collection

A mock server created from a collection returns saved examples when it receives a matching request. When you send a request to the mock server, Postman uses a matching algorithm to select the best example and return its response. You can provide multiple examples to handle different scenarios.

If you save the URL of a mock server to a variable, you can reference it across requests. For example, if you have a production server and a mock server, you can have an environment for each. In each environment, create a variable with the same name (for example, {{url}}). By using the variable in your requests, you can switch between environments to call either server.

For additional topics, see Make calls to a private mock server, Use HTTP access control, View the mock server call log, and Troubleshoot mock calls.

To make a call to a collection mock server, do the following:

  1. Click the Services icon Services tab in the sidebar, then expand Mock Servers.

  2. Hover over the server, then click Copy icon Copy Mock URL. Or, click the server, then click Copy icon Copy Mock Server URL in the workbench.

  3. Open a new collection request (or edit the address in an existing request), then add the mock server’s URL. The URL includes the mock server’s ID and the path to the request you want to mock. For example, https://4bb57fc2-219e-421e-86b4-4ffda6bf1b3b.mock.pstmn.io/get.

    Make sure the request you want to mock has at least one saved example. Learn more about adding examples to a request.

  4. If your mock server is private, add your Postman API key to the request as an x-api-key header. See Make calls to a private mock server to learn more.

  5. Click Send to send the request.

If you configured a delay for your mock server, Postman waits the specified period of time before sending the response.

Make calls to a private mock server

To make calls to a private mock server, you need to add a Postman API key in the request header when sending requests to the mock server (for example, x-api-key:<Your-Postman-API-key>). If you share the collection, others can use their Postman API keys to make calls to the mock server.

Use HTTP access control for a mock server

In addition to using Postman to call mock endpoints, you can also make requests to a mock server using a web browser.

A web browser makes a cross-origin HTTP request when it requests a resource from a domain, protocol, or port that’s different from its own. Cross-Origin Resource Sharing (CORS) is a standard that defines a way in which a browser and server can interact securely. In this case, CORS refers to how a web browser interacts with the mock endpoints hosted on the Postman mock server.

CORS is enabled for Postman mock servers, so you can stub your web apps with mocked data using the mock endpoints. Development or production web apps can then make requests to your Postman mock endpoint and receive example responses.

View the mock server call log

You can view and search the details of calls to your mock servers using the mock call log. To view the call log, click Mock servers in the sidebar and select a mock server.

Viewing mock calls

The call log displays a list of calls made to the mock server URL. Each entry shows the time the request was sent, the request method and path, and a response overview.

With mock server call logs, you can:

  • Enter text in the search box to find specific requests.
  • Click Refresh icon Refresh Logs to view the most recent requests.
  • Select an entry for more details about the request headers and body or the response headers and body.
  • Click Delete icon to remove a call log entry.
  • Click Copy icon Copy Mock Server URL to copy the mock server’s URL. You can use it to make calls to the mock server.
  • Click Docs icon View Collection Docs to view the mocked collection’s documentation.
  • Click Link icon Copy link to mock server to copy a link to the mock server.
  • Click Setting icon Edit Configuration to edit the mock server’s configuration.
  • Click Options icon to view more options:
    • Manage Roles — Manage the mock server’s user roles and permissions.
    • Move — Move the mock server to a different workspace.
    • Delete — Deletes the mock server.

Troubleshoot mock calls

You can use the mock call log to troubleshoot your requests to mock servers.

If No matching requests displays in the Response column, this might mean there’s a problem with your mock server configuration. Make sure there’s an example saved for the request in the collection you’re mocking.

In the case of a service outage, you will get an HTTP 502, 503, or 504 response. Check the Postman status page for updates.