Postman Examples Dynamic Response Based on Request Data

1.5k Views Asked by At

Probably missing something completely obvious in the docs but is it possible to echo request data in a Postman Example / Mock Server response based on the input request.

Example Request:

POST:

{
    "firstName": "{{$randomFirstName}}",
    "lastName": "{{$randomLastName}}",
    "phoneNumber": "{{$randomPhoneNumber}}",
    "email": "{{$randomExampleEmail}}",
    "employeeId": "{{$randomInt}}"
}

Intended example response:

{
    "id": {{$randomInt}},
    "firstName": "{{$req.firstName}}",
    "lastName": "{{$req.lastName}}",
    "phoneNumber": "{{$req.phoneNumber}}",
    "email": "{{$req.email}}",
    "employeeId": "{{$req.employeeId}}"
}

1

There are 1 best solutions below

0
On

I see you want to use Postman's dynamic 'faker' variables in the request body to be returned in the mock response. A similar use case is supported for the request URL (refer to the section on 'wildcards' here) but not body.

Here's one way to achieve this with the request body:

  1. Create an environment 'e1' with variable 'firstName'.
  2. Edit your mock to add the environment 'e1'.
  3. Use the same environment variable {{firstName}} in the response body of your example.
  4. Dynamically update the value of 'firstName' before sending the mock request. If you're using the Postman client, you can do this using the pm.environment.set method. If not, then you can use the Postman API to do this.

On the other hand, you can simply use the same faker variable {{$randomFirstVariable}} in your mock example response as well, but the value returned could differ from the one sent in the request.