Reputation: 550
I got a response from a get request as an object and added it to an environment variable. now I want to use it in another request params but I don't know how to do it.
Upvotes: 3
Views: 1148
Reputation: 36
You should use JSON stringfy in order to save in the environment. after that use the Pre-request Script in order to add query params and JSON parse to get data from the environment. follow these steps as a sample code.
1- pm.environment.set("key", JSON.stringfy(object))
2- go to Pre-request Script
3- var data = JSON.parse(pm.environment.get("key"))
4- pm.request.url.addQueryParams([`param=${data.child}`])
Upvotes: 2
Reputation: 680
environment variables are just text, so you might need to strigify the json response object and parse it before executing the next request.
There are pre-requests tests that you could use to read and parse the environment variable.
https://learning.postman.com/docs/writing-scripts/pre-request-scripts/#pre-request-scripting-example
Upvotes: 2