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
// 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.
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!
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
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
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');
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.
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!
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)
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!