Can you use values from the request body in the response body?

I want the Postman’s mock server respond to a POST request containing JSON values from the request body.

For example,

request:

{
  "firstName": "John",
  "lastName": "Smith"
}

Response

{
  "existing": true,
  "person":  {
    "firstName": {{firstName}},
    "lastName": {{lastName}}
  }
}

Would be great if this exists, so we can push our API-first further

Hey @sjeefr ,

Perhaps there is a better approach than the one I’m suggesting however hopefully it may help you. I’m not entirely sure you’ll be able to accomplish your use case with the mock, but you could also make use of Postman Echo by using the post endpoint https://docs.postman-echo.com/?version=latest#083e46e7-53ea-87b1-8104-f8917ce58a17 That endpoint should echo the contents of the request body and thus helping you test.

The following suggestion may be better suited for your needs. You could leverage the use of Dynamic Variables i.e https://learning.postman.com/docs/postman/variables-and-environments/variables-list/#names to generate a random first name and random last name in your example response, e.g:

{
"firstName": "{{$randomFirstName}}",
"lastName":"{{$randomLastName}}"
}

You’d then create environment variables such as randomfirstName and randomlastName and set the request body to something like:

{
  "firstName": "{{randomFirstName}}",
  "lastName": "{{randomLastName}}"
}

And the final step would be to set up your test scripts to set the respective values of randomFirstName and randomLastName from the response body. Example:

var jsonData = pm.response.json();

pm.environment.set("randomFirstName",jsonData.firstName);
pm.environment.set("randomLastName",jsonData.lastName);

Hopefully, you found this information helpful and useful.