![]() ![]() ![]() This prefix could be changed to match one that you may use in your collections. The end result of running the script, which will run after the request has been made, is that it clears out the environment variables that start with the “demo” prefix. It’s the ones with the prefix that we will be clearing out, after the request has been made.īefore the request is sent, the environment variables can seen using the environment quick look feature. I have a mixture of variables here, some using the “demo” prefix and some without. In a more realistic workflow, these variables would have been created during a collection run, using the `pm.t()` function. To unset the variables with the “demo” prefix, I’ve added an `if` statement and used the `startsWith()` method to grab the ones I want.įor demo purposes, I’ve manually added these variables into an environment file to demonstrate what the script is doing. I’m using _.keys() to get a list of all the keys within the `pm.environment.toObject()` object and then using _.each() to iterate through these. I wrote in a previous post about using the momentjs module, which is built-in to the native Postman application, to create this cleanup I’ve used another great module Lodash, this is an awesome utility module which just makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc. Whenever I’m trying to do anything in the `Tests` or `Pre-Request Script` Tabs, I will always take a quick look at the Postman Sandbox API reference page to see if there was anything that I can use – The `pm.environment.toObject()` method popped out at me, this was something that would give me the dynamic element I needed so I wouldn’t have to hard code any values within an array. I wasn’t aware of how to do this dynamically so I thought it was a great opportunity to learn something new. It was something that I had done before in a manual way, by adding hard-coded string values to an array and then iterating through the list to unset each one. ![]() I answered a question on StackOverflow recently about creating a solution to dynamically clear out certain environment variables, that had been set during a collection run. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |