Updating Path Variable in URL before posting

Hi!

This may be a simple answer but I just am not seeing it. I have a GET request with a simple URL, I managed to create Path variables that I can populate in Postman with no issues using : before the Variable name in the URL.

I am trying to strip away any whitespaces from a Path Variable I called Postcode, and so far so good, I can see the successful transformation in the console. Below is my Pre-request script;

var CorrectedPostcode = pm.request.url.variables.get('Postcode');
CorrectedPostcode = CorrectedPostcode.replace(/\s+/g, '');
console.log(CorrectedPostcode);

What I want to do is now update this Postcode path variable to the new CorrectedPostcode value?

Closest function I found was pm.request.url.variables.substitute but for the life of me cannot find an example of this being used

The following appears to work…

// Step 1 - Retrieve current URL information
const url = pm.request.url.toJSON();

// Step 2 - Retrieve index for the key we want to update
// Path variables are stored in the variable array
const index = url.variable.findIndex(item => item.key === 'Postcode');

// Step 3 - Update Path
let correctedPostcode = pm.request.url.variables.get('Postcode');
correctedPostcode = correctedPostcode.replace(/\s+/g, '');
url.variable[index].value = correctedPostcode

// Step 4 - Finally replace URL with our updated object
pm.request.url = url;

What I found is that you can’t interact directly with the “variable” list via the pm.variable.url function like you can with some of the other elements in the function like the host or path. Suspect it because is actually a JavaScript list rather than an array (or is read only via this method).

But what I could do, is to convert the pm.request.url object to JSON as its own variable.

Update that variable, and then overwrite the entire URL with the updated object.

Initially, I had it overwriting the path variable by its index number, but I think that overwriting the value for the specific key, and then replacing the entire URL object looks cleaner.

If the variable is in the path, it should just be as simple as updating the variable in your pre-response script.

Assumption: At the time you run this request you already have the postcode you want, you just want to do some cleanup on it.

You can make this a local variable and set the cleaned up data in a pre-request script.

Code for copy/paste:

let postcode = pm.variables.get('Postcode');
postcode = postcode.replace(/\s+/g, '');
pm.variables.set('Postcode', postcode);

This code fetched the value of the variable out of wherever it’s stored (you could do pm.collectionVariables.get or pm.environment.get based on your implementation), does the modification, then overwrites the saved value.

Keep in mind, my code above is setting a local variable which is the narrowest scope of the variable types. The variable only exists during execution of the request. But anyway, that’s how you do what you’re trying to do!

Good luck!

1 Like

The OP will have to clarify, but I think the OP was talking about Path Variables, like in the following screenshot.

pm.request.url can surface these values…

The Path lists the Key’s, the variable has the Key and Values.

You can’t update the variable key directly.

You can update the path key directly.

This is what I think the OP is trying to update.

I guess if it was a variable as in your example instead of using a path variable, it would be easier to update.

You can also use a variable as the value in the Path variable, so that might be an option.

1 Like

Hi Allen,

Thank you for the response, but I did try this before and it did not work. You see I didn’t create a variable in the environment, this is a URL Path Variable, which I normally update before the call on the Params section in the postman call. This lets me quickly copy/paste a postcode and check it against an API

image

I did figure out a crude workaround using pm.request.url.update and essentially rebuilding the entire URL in the pre-request script, but was wondering if there was a way to update a pm.request.url.variables variable type, as no matter what combination of regular pm.variables I use it never does anything as it seems the URL params are not counted as variables in the same way collection or environment variables are

@supply-technologist6

I don’t think you need to rebuild the entire URL.

The problem is that the variable key in pm.request.url that contains the path variables cannot be written to directly.

What you can do, is create a local variable with the information from pm.request.url.

Update that variable, and then save that variable over the top of pm.request.url.

The variable key is an array, so you need to know the array index of the key so you can target and update.

I provided an example in an earlier post that appears to work.

Hi Mike,

Thanks for that! I suppose it would make sense why the URL can’t be updated in the same way… I do believe I found a solution that works in my case;

var Postcode = pm.request.url.variables.get('Postcode');
var DestinationCountry = pm.request.url.variables.get('DestinationCountry')
var OriginCountry = pm.request.url.variables.get('OriginCountry')
Postcode = Postcode.replace(/\s+/g, '')
pm.request.url.update('{{host}}/postcode/api/check/'+Postcode+'/'+DestinationCountry+'/'+OriginCountry)

This produces an updated URL though for some reason I had to turn the other Path Variables into local variables here as well, as just leaving :DestinationCountry in the URL string as normal resulted in the Path Variable not being read

Your solution also works perfectly, I just want to know why “item” is used in this part;

const index = url.variable.findIndex(item => item.key === 'Postcode');

is that just a default like “any”?

Item is just a variable, it could be anything. It’s just used while the findIndex function iterates over the array. I normally use obj, if I’m iterating over an array of objects, or item, if its a flat list. Personal preference.

I have one more dirty way of achieving this in a one liner.

pm.request.url.path[pm.request.url.path.indexOf(":Postcode")] = correctedPostcode;

This is overwriting the path variable completely with a static value. (Your updated postcode).

1 Like

Hi Mike,

I see that makes sense! I appreciate the help, with your help the whole script is now 2 lines;

let correctedPostcode = pm.request.url.variables.get('Postcode').replace(/\s+/g, '');
pm.request.url.path[pm.request.url.path.indexOf(":Postcode")] = correctedPostcode;
2 Likes

I still feel like you’re thinking about this the wrong way. Set the path variable to a variable (whatever scope you want).

To me, what you guys are talking about feels like a weird workaround. But if you’re happy with the solution, then I’m happy too. Glad you figured it out! :blue_heart:

1 Like

Hi Allen,

I do understand that using the environment variables is probably easier, though this would not work in this scenario - the end goal is to be able to input a postcode in the Path Variable value field (since its easier to access than going to the environment tab, finding the environment I’m planning on using, and then passing it there)

image

Thank you for your input however, its my first post on this forum and I am so glad I was able to get multiple angles to look at this from!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.