Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postman - Use part of the response data from one test in another test

Tags:

postman

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?

like image 658
Archer Avatar asked Jun 19 '18 08:06

Archer


1 Answers

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.

Postman

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])
})

Postman

like image 150
Danny Dainton Avatar answered Nov 09 '22 06:11

Danny Dainton