Reputation: 125
I require help to execute a postman test which requires a response output from another test. I have checked with various forums but a solution is not available for this particular scenario.
Example
Test 1 response:
{
"items": [
{
"email": "[email protected]",
"DocumentName": "tc",
"type": "URL",
"url": "https://localhost:8443/user/terms?statusno=a5f2-eq2wd3ee45rrr"
}
]
}
Test 2:
I need to use only the a5f2-eq2wd3ee45rrr
part of the response data from Test 1
, this can be seen in the url
value above. I need to use this value within Test 2
How can I make this work with Postman?
Upvotes: 8
Views: 7072
Reputation: 25891
Not completely sure what the response data format is from the question but if it's a simple object with just the url
property, you could use something simple like this:
var str = pm.response.json().url
pm.environment.set('value', str.split('=', 2)[1])
This will then set the value you need to a variable, for you to use in the next request using with the {{value}}
syntax in a POST request body or by using pm.environment.get('value')
in one of the test scripts.
Edit:
If the url
property is in an array, you could loop through these and extract the value that way. This would set the variable but if you have more than 1 url
property in the array it would set the last one it found.
_.each(pm.response.json(), (arrItem) => {
pm.environment.set('value', arrItem[0].url.split('=', 2)[1])
})
Upvotes: 9
Reputation: 579
If you get JSON response and then send JSON in body, I would do the following:
1) In test script(javascript):
var JsonBody = pm.response.json();
var strToParse = JsonBody.url;
var value = strToParse.slice(indexOf("?status=")+"?status=".length);//parse string
//manually but you can google for a better solutions.
pm.environment.get("varName" , value)
2) You can use it! In scripts like: pm.environment.get("varName")
, and everywhere else using {{varName}}
Upvotes: 0