Reference requests and examples in code mocks
The pm.mock object provides structured, Postman-aware functions for matching incoming requests and sending responses with a code mock. The pm.mock API can serve responses from your existing saved Postman examples rather than hard-coding everything.
pm.mock
Use the pm.mock methods to match incoming requests and send responses, including responses from your existing saved Postman examples.
In the following examples, <request-path> and <example-path> are placeholders for the path to a saved request and example in your local Git repo. The example’s status code, headers, and body are all sent as the response.
Rather than looking up paths manually, Postman provides a searchable dropdown directly in the mock code editor. When you click the argument, a dropdown list displays where you can search your workspace’s requests and examples by name or path.
pm.mock.matchRequest()
Matches an incoming request against a Postman request by its method and path. Returns true if the incoming request matches the specified criteria.
Example:
pm.mock.sendExample()
Sends a saved Postman example as the HTTP response. This is the key integration point between your existing Postman collection data and your code mock.
Below is a complete example using the pm.mock API:
Path variable matching
The matching algorithm supports path variables, which are URL segments prefixed with : that match any value in that position.
Example: