![]() It’s very useful when you have a collection of different requests set up with variables. In the following example we set the fmc_access_token environment variable from header “ X-auth-access-token” and DOMAIN_UUID variable from header “ DOMAIN_UUID” using Tests:Īnd now we have those variables as CURRENT VALUE in our environment ready to use in other requests: The response with the Token and Domain looks like this: Step 4: Now Click on the Environments tab, and you will find a new auto-generated Environment for the newly created collection. Using Tests in the Token Generation request we can set the Variable from the body/header and it will be set as a normal Environment Variables, no more copy-paste. I used to just copy it from the header into all other requests.īut there is a better way to do it. Sometimes the service itself generates Access Token and provides in the body or header. Setting up Postman Environment Variables: Postman supports variables at different scopes, allowing you to tailor your processing to a variety of development, testing, and collaboration tasks. I will use Cisco Sandbox for my examples, that is one of the great ways to get some practice with Cisco products API. Postman makes this development phase much easier.ĪPI is getting more to the token-centric authentication and Environment Variable becomes handy. Variables can also be grouped into a Postman environment. Something very quick and easy to create but hopefully others will find some benefit from using it within their own context.Using Environment in Postman is one of the best things that we can use during the testing and while playing around with API. Setting up Postman variables Variables in Postman allow you to store and reuse values rather. As I love to learn and my JS knowledge is still at a novice level, I’ll be happy for someone to make the code more efficient. Besides the sandbox environment tied to your developer account, you can use Postman to test REST API production code and endpoints. The code snippet can be found at the link below, please feel free to use and modify it to suit your needs. When the ‘key’ matches the `if` statement condition, it’s placed into the `pm.environment.unset()` function and removed. I’ve added a `console.log(arrItem)` statement to the code to show in the image, the keys that have been iterated through, while the script was running. Create Variables Here we are defining important variables to store and reuse values throughout this collection. 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. Inside Postman test scripts, you can set environment and global variables using the tEnvironmentVariable and tGlobalVariable functions. 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. Global variables in Postman will need to be. 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. Postman variable types Environment variables in Postman will need to be defined for each environment in mabl. 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. |