Set Variable from Parsed Header Location Path

I have several calls in a workflow that don’t return body data in the response and I need to set the last part of the location URL path as an environment variable to use in the next call.

Ex.

  1. Request: Create package - Response: URL/packageID (packageID is what I want to set as a variable)
  2. Request: Create document inside of package (use package ID in post) - Response: URL/documentID (documentID is what I want to set as a variable)
  3. etc.

I’ve searched the community

I’ve reviewed

and I’ve looked through
https://www.postmanlabs.com/postman-collection/HeaderList.html

…but still don’t understand how to get just one part of the URL to set as a variable from the Header Response > Location

Below is a screenshot of the part of the URL I want to set as a variable. Help from the community would be much appreciated.

  • Thank you!

Hey @atepe

You could try something hacky like this in the Tests script:

let header = pm.response.headers.get('Location')

console.log(_.last(header.split('/')))

Here it’s getting the value of the Location header, it’s splitting the string on the / character which creates an array. The _.last() past is just returning the last value in that array as it looks like that’s the thing you need.

I’ve just logged the value to the console in that example but saving it to a variable would be something like:

let header = pm.response.headers.get('Location'),
    id = _.last(header.split('/'));

pm.environment.set('documentID', id);
1 Like

Thank you so much! This worked perfectly

1 Like