How can I put variables into a POST request Response body?

Greetings, I’ve created a mock server and am trying to make some more dynamic responses. I’ve got a post request that I want to essentially respond with a random number. Ive got the variable set up in the environment and in the pre request script have it generating a random number which is fine, but when I go to the response body and put into it: “{{randomNumber}}” and after sending the request I get returned an empty string. I can see in the environment quick look that a random number is definitely being generated but for some reason will not put that variable into the post request’s response body.

Mock servers rely on saved examples of requests and responses. I don’t believe you can use a saved example with a pre-request script to generate a random number that is returned in the mock response, in that way.

Instead, you could use a Faker variable, like {{$randomInt}} and save it in the example’s response body, like this screenshot here.

And a couple more ideas for generating “dynamic” behavior: Fake it till you make it: mocks for agile development | by Joyce Lin | Better Practices | Medium

Thanks for the reply. I’ve just been trying to get a random number between 1-10 rather than the {{$randomInt}} which is 0-1000. I kind of got it working by turning on the persistant variables but it changed to the first random number generated and stayed that way.

You should be able to use a pre-request script on the actual request to generate your random number. Which you can save to an collection variable that can be used in place of $randomInt.

Postman has the Lodash library available in the Sandbox and you can use the Lodash random function.

pm.collectionVariables.set("randomInt", _.random(10));


Here is the response body:

And then here is what i get in the response:

In the env quick look i can see it is a number and its being stored:

I haven’t tried this myself, so there is an assumption that you can do this the same as you can with the faker variables.

I’d have to set this up myself to prove it.

CollectionVariables are generally stored as strings so try removing the quotes for that element in the response and see if that makes any difference in the first instance.

Ok, so I’ve tested this and it doesn’t work.

The reason its not working is because the mock doesn’t seem to have access to the current values for collection or environment variables.

Which is probably correct as the mock runs from the Postman Cloud.

If you set a value in the initial value for a collection variable. It works, but this isn’t of much use in this scenario.

You can update the initial value using the Postman Collection API, but that seems overkill for what you are trying to achieve.

2 Likes

just so i can check it out, how can I update the initial value using the collections?

Set initial value of collection variables - :person_raising_hand: Help - Postman Community

image

You need to generate and include an API key in the headers (X-API-Key).

The above is a full request, but you can use sendRequest() in a pre-request script to the same effect.