By default, Postman mock servers return static responses defined in saved examples. To return responses that have random data, or that vary based on the request, you can generate dynamic responses. With dynamic mock responses, you can simulate various scenarios without having to create a saved example for each one.
You can store values in variables and use them in your requests and collection examples. If you change the value of a variable, the new value is used wherever the variable occurs.
Postman mock servers support environment variables and collection variables. (Mock servers don't support using global variables or vault secrets.)
When you use an environment or collection variable in an example, the mock server resolves the variable and replaces it with the variable's initial value. If an environment variable and a collection variable have the same name, Postman uses the environment variable. Learn more about variable scopes.
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 server. In each environment, create a variable with the same name for the mock URL. By using the variable in your requests, you can switch between the two environments to call the production server or the mock server.
To have your mock server return random data, use dynamic variables in your example's response body. Dynamic variables are resolved as part of the mock server response and replaced with random data. Dynamic variables are useful for generating random data when mocking an API. Use them for exploratory testing and writing rich, data-driven tests.
For instance, your example's response body might contain dynamic variables like the following:
{
"name": "{{$randomFullName}}",
"userName": "{{$randomUserName}}",
"location": "{{$randomCity}}",
"company": "{{$randomCompanyName}}",
"jobTitle": "{{$randomJobTitle}}",
"updatedAt": "{{$timestamp}}"
}
When you call the mock server endpoint, the response data will change to something like the following:
{
"name": "Cielo McClure",
"userName": "Aurelie.Lockman",
"location": "Kubhaven",
"company": "Runolfsdottir, Bernhard and Hodkiewicz",
"jobTitle": "Direct Branding Liaison",
"updatedAt": "1565088856"
}
See Use dynamic variables to return randomly generated data for a full list of dynamic variables for generating random data.
With template support, Postman mock servers can generate responses that vary based on the incoming request. Template helpers give you access to data from the incoming request, such as the body, query parameters, path segments, and headers. You can include this data in the response sent by the mock server.
To generate contextual responses on a mock server with response matching for the request body enabled, you need to set the
x-mock-match-request-body
header set tofalse
. Otherwise you will get the errormockRequestNotFoundError
.
To create contextual responses, add one or more template helpers to an example in the mocked collection. You can use the following template helpers in your examples:
$body
- Access the body of the incoming request$queryParams
- Access the query parameters of the incoming request$pathSegments
- Access the path segments of the incoming request (such as /product/id/details
)$headers
- Access the headers of the incoming requestUse object-path syntax to access specific values in the helpers. You can also define a default value for a helper in case the mock server can't resolve the variable. The following table shows some ways you can use helpers in your examples.
Template helper | Data returned |
---|---|
{{$body}} | Return the full request body |
{{$body 'path.to.property'}} | Return the value of a specific property from the request body |
{{$headers 'header-key'}} | Return the value of a specific request header |
{{$queryParams 'parameter-key'}} | Return the value of a specific query parameter |
{{$pathSegments '1'}} | Return the second segment of the request path (for example, if the request path is /product/12345/details then return 12345 ) |
{{$body 'property' 'default value'}} | Define a default value for a property |
{{$body 'a\.a'}} | Return the value of the property a.a which has a dot (. ) in the key name |
This example shows how to use a template helper to access data from the body of the incoming request and return that data in the response from the mock server.
In the mocked collection, create a new request, then select the raw option in the Body tab. Select JSON from the dropdown list, then enter the following:
{
"username": "postman",
"password": "12345"
}
Add an example to the request. Then add the following body data to the example. The {{$body}}
template helper is used to access the username
value.
{
"username": {{$body 'username' 'postman'}},
"id": {{$randomUUID}}
}
Send the request to the mock server using different values for username
in the request body.
For example, if you send the following request body:
{
"username": "s-morgenstern",
"password": "12345"
}
Then the mock server will return a response with the username
value from the incoming request:
{
"username": s-morgenstern,
"id": 1ad6b425-5ebf-4864-98e0-7bb44c318bac
}
Last modified: 2024/10/18