The ability to set the data from the console log as a variable for the next request

Hello ! May be someone can help me)

What i want:

  1. I have a collection variable on POST/ request
{  "Amount": .....,
    "Id": ........,
    "customerId": "{{$randomUUID}}",
  }
  1. To cath the value of this variable to use it in the next POST request i need to log it in console so i have used script:
const requestJson = pm.request.toJSON();
pm.collectionVariables.set('aRequestAsJson', JSON.stringify(requestJson));
const x = JSON.parse(pm.collectionVariables.get('aRequestAsJson'));
console.log(JSON.parse(x.body.raw).customerId);

and the UUID that were used in request body is successfully logged in my console
and presented as :


POST https://............
"fb8c4dd4-5e31-4620-b94f-849dffb0d2fd"

But how can i set that console data(i mean fb8c4dd4-5e31-4620-b94f-849dffb0d2fd) as a collection variable to use in my next POST/ request ?

I trying something like that:

const requestJson = pm.request.toJSON();
pm.collectionVariables.set('aRequestAsJson', JSON.stringify(requestJson));
const x = JSON.parse(pm.collectionVariables.get('aRequestAsJson'));
console.log(JSON.parse(x.body.raw).customerId);
pm.collectionVariables.set("UuidForTheNextPostRequest",  (x.body.raw).customerId);

but it doesnt works(

I’ve created this Collection as an example of using the pre-request to set and store the random value. This can then be used in other requests.

https://www.getpostman.com/collections/06fb7486cd2dcd743645

2 Likes

@dannydainton Interesting, I’ve not used pm.variables.replaceIn yet, is there anywhere that explains the use options? Cheers

The learning center explains most things - If it doesn’t we can change that :smiley:

2 Likes

Wow, great solution ! Thanks a lot !

1 Like