Dynamically unset Postman Environment Variables

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. 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 wasn’t aware of how to do this dynamically so I thought it was a great opportunity to learn something new. 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 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.

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. 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.

Postman_Function

For demo purposes, I’ve manually added these variables into an environment file to demonstrate what the script is doing. In a more realistic workflow, these variables would have been created during a collection run, using the `pm.environment.set()` function.

Env_Vars

I have a mixture of variables here, some using the “demo” prefix and some without. It’s the ones with the prefix that we will be clearing out, after the request has been made.

Before the request is sent, the environment variables can seen using the environment quick look feature.

Env_Quick_Look

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. This prefix could be changed to match one that you may use in your collections.

Result

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. When the ‘key’ matches the `if` statement condition, it’s placed into the `pm.environment.unset()` function and removed.

The code snippet can be found at the link below, please feel free to use and modify it to suit your needs. 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.

https://gist.github.com/DannyDainton/0a204d53e72464029a5aca8b2ae1592f

Something very quick and easy to create but hopefully others will find some benefit from using it within their own context.

 

Advertisements

Hold on, wait a moment…

After using an application like Postman for a while, I’m always still pleasantly surprised when I stumble upon particular features that I never knew were included in the native application. Postman comes with many cool features out of the box and one that’s included is an awesome ‘time saver’.

The Pre-request Script and Tests tabs are like a mini JavaScript playground and expand on the already amazing things that you can achieve with the tool. when testing Restful APIs. I’m a fan of JavaScript and being able to use my limited knowledge of that language within Postman has been a huge help to me, what I’m not a fan of is working with anything date and time related using native JavaScript – It’s just painfully horrible and sometimes confusing.

I’m always creating little Node.js applications and helper tools to assisting my testing, to help produce things like a ton of test data, far quicker than I can manually. Some of this data includes dates and times in all different kinds of formats – I make use of the moment.js library, this is just an awesome utility module that reduces the pain of working with anything time/date related.

I was very pleased several months ago, when reading through Postman’s documentation, to discover that this awesome module comes built-in with the native client! Win!

How to start using moment within Postman?

If you’re familiar with Node.js and the way that you reference external modules in your scripts, you’re basically halfway there…If not, don’t worry its super simple – all we need to do is write the following line in the Pre-Request Script or Tests tab, depending on where you would like to use it.

reference_moment
Referencing the external module

Postman already knows what the moment module is so we don’t have to install this and save it anywhere, we are just basically telling the application that we’d like to make use of this within our test script. Now that we have made the reference to the module, we can use the ‘moment‘ variable to access all the awesome features!!

I’m going to show you a few ways that you can use this within your requests to give you a flavor of what you could do with it and once you’re comfortable with the syntax, you can start to explore the documentation a little bit more and find some new cool ways to start making use of this in your own context.

To be honest, if all you’re after is an ISO date format, there is no real benefit bringing in the external module – This example would create the same time object using either the native JS or the moment way.

Basic_moment
Basic datetime object

Where I feel moment brings value is when you want to add some formatting to the time object to suit a specific endpoint when POSTing data or you need to add a start or end time to a URL parameter filter etc. The way that moment chains the different functions together makes it easier, as a human, to read the syntax and have an instant understanding about what it’s actually doing. I don’t personally feel that you get this when using the native JS syntax.

Formatting the time object is very simple using moment – There are lots of different options available to use, a full list can be found here. I’ll show you a couple of options below:

Moment_Format
Different time formats

This shows a selection of different formats that can be easily created – There are far too many combinations to show you here but their should be something in their to suit your needs, when making requests in Postman.

I mentioned that we might want to add some time values to certain URL parameters, for our requests – The image below shows this can be done using the ‘add‘ and ‘subtract‘ functions, this is all chained together to make things easy to read.

Add_Subtract_Time
Adding and Subtracting time

This is shows a time value created in 3 different ways – 10 minutes in the past, now and finally 10 minutes in the future. I’m just using ‘minutes‘ in this example but this could be seconds, hours, days, weeks, years etc.

All the basic examples have just logged these values out to the Postman Console, let’s quickly look at how we could use this on the requests that we are making. The best way is to store these as either an environment or a global variable – Once saved, you will be able to reference this value and use this within the different parts of your request.

Global_Time
Set as a Global Variable

This example creates a Global variable with the moment ISO date time as the value – This was created after the request so it’s not that useful but if we were to add this to a Pre-Request Script, which executes just before the main request, we could reference this value in a POST request body.

Request_Body_Moment
Request Body showing the created time object

The same method of creating variables, this time an environment one, could be used in the Pre-Request Script to create some dynamic URL parameter filters.

Pre_Request_Dates
Set the variables in a Pre-Request Script

This example would create the variables before the request is sent and it will use these 2 time object values in the URL parameters which would, in theory, give you a 1 hour time window.

These are very basic use cases but hopefully this will give you an idea of how and where you could use the moment module in Postman for all of your time based needs. If there is anything that you’re trying to achieve and you’re still unsure of how this all pins together – Please feel free to add a comment or you could reach me @dannydainton on Twitter. I’m always happy to help out.

I’m still currently adding different Postman related content to this GitHub repo, hopefully some of this infomation is useful to you. It’s an ongoing project so it will never be a ‘finished’ resource.

Hi there – I’m here to help…

 

 

I’m obsessed….I can freely admit that and be perfectly comfortable with saying it! The object of my unhealthy obsession is Postman – If you know me and have been following any of my work lately, you’d know that for sure. I’m always talking about how awesome it is as a tool and I’m also creating free content in a public Github repo to help others learn more about the tool and all the different wonderful ways to use it.

So I’ve established that I’m into Postman in a big way – I’m always looking to help people with any questions they may have with using the application, no question is too small. The trouble that I’ve found is that very few people actually approach me, which is totally fine but because I’m naturally a helpful person….in a totally weird way I would love to have a ton of problems to try to get my head around. I love challenging myself and knowing where my limits are, I’m still learning as I go so it’s great to just evaluate where I’m currently at with my knowledge.

Last month, as I was researching for a new Postman example that I was writing, I was stuck on a particular problem and like many people in that situation, I turned to Google. When the results of my search came back I was surprised to see lots of links to Stackoverflow – thinking about it now it seems perfectly reasonable, It’s a tool that has been used by millions of people in the world and has now been around for several years…people were bound to have questions about how to do certain things.

Just a bit of background about my previous encounters of Stackoverflow – I’m always tinkering with different applications or different programming languages so when I’ve searched online for help with a problem, that site has been the main source of my information. It’s probably the go-to place when you have a development type problem to solve. I’ve asked a couple of questions on there in the past and got an answer extremely quickly…It saved me days of banging my head against the wall!!

The majority of the questions on the site are ‘tagged’ – If the question related to a problem with a Node Express API, it would be tagged with something like ‘javascript’, ‘node.js’, ‘express’, ‘api’ etc. The more the question is specifically tagged, the more reach it will have and potentially be answered quicker.

Getting back to Postman…I started to use Stackoverflow’s search feature with the ‘postman’ and ‘postman-collection-runner’ tags applied – this brought back a whole host of questions that I could instantly answer, some new and some old. Yay! I had a new outlet for my obsession! Postman is a relatively niche topic on the site, it’s referenced a lot because people will use it while developing and testing API’s or Web Services so it will be mentioned in thousands of questions but as a topic, there has only been ~2500 questions tagged.

The whole Stackoverflow site is built on a model of reputation, the more questions you answer the more reputation points you get – You can also get points for many other things like up-votes, editing posts etc. It gamifies the whole process and as well as wanting to help others, you also want to build up your reputation and probably your personal credibility on the site. As I was a new user I had a score of about 10 I think, I got these points from the 2 questions that I asked a couple of years ago. I wanted to set myself a target of getting up to 500 points – I thought that was quite reasonable for someone just answering questions about a single tool….I didn’t expect to learn as much from helping people, as I did, in that short amount of time.

The very first problem that I faced as a new user to the site was that because I had a reputation of under 50, I wasn’t allowed to comment on any of the questions – Why was this such a big problem? Think about the worst bug report you’ve ever seen…Something so vague, void of details, impossible for you to reproduce given the information and just basically a load of crap. That’s the level of some of the questions asked by users on the site, seeking an answer to a technical problem…The ability to comment gives you a place to seek clarification and to tease out more details but you can’t even do that until you’ve gained enough points to be able to do it – Which absolutely sucked!!

Thankfully, I answered a few basic questions and got some points on the board so I could then extract more information via the comments section so that I could actually help people. Over the course of about a month, I’d done myself proud – I’d answered a bunch of different questions of various degrees of difficulty, using the same method as I have been doing when explaining the different Postman features in my Github examples and in turn I’ve helped many people but above all, learnt a bunch of new stuff along the way.

I didn’t manage to reach my 500 point target but I got pretty bloody close!! I’m still checking in on the site but I’m going step back a bit from it now and concentrate on my upcoming Testbash talk in Brighton.

 

 

A sample of some of my answers that I gave:

Update:

I continued to answer questions that other people have asked on the Stackoverflow site and I’ve just broke through the 2000 point mark…very proud! I think i’m going to take a step back now and concentrate on something else – As it stands, I answered 119 questions so i’m very happy that I could help that many different people.

SO_Rep

 

Just fiddling around…

This month I started a mini project on Github to create a small knowledge base, all around the REST Client tool Postman. I’ve been using this tool for a while now and I’m a massive fan, I want to share some of the knowledge that I have gained, with other people.

It’s basically a list of examples that use the tool and its many cool features to interact with a public API. This wonderful resource has been created by Mark Winteringham. Mark has created Restful-Booker, a safe place for people to learn more about API testing and an active platform to try out tools like Postman.

In one of the examples, I explain how to the use Manage Environments feature. This will allow you to create an environment file, then assign pieces of data to variables. That data can then be referenced in any of your Requests within a Collection. This is very handy during the creation of an API, where you may have different environments to test the API like development, staging, pre-production etc. The routes of the API will generally stay the same but the baseURL will change depending on the environment location. Check out the example to learn more about this in more detail.

So why write a separate blog post?

I’m always fully open to learning new things and sometimes you stumble across things by accident or as the result of looking into something else. I love creating visual helpers when I’m trying to explain something – “A picture paints a thousand words”.

To fully explain what I meant by the different environments in the ramblings above, I thought I would just create a couple of super basic Nodejs Express APIs locally and then edit the Hostfile on my local machine to override the DNS for the localhost domain, so that I could show requests being made to dev-restful-booker and staging-restful-booker in Postman.

The code for each API is crazy simple, I wanted to mimic the actual Restful-Booker API so I added the Content-Type header and also made it return a 201 Created status code. The only real difference between my mock dev and staging APIs was the .send() value and the port number that is was running on.
test_server
Mock API Code 

So I added the names to my local Hostfile and started the APIs…that’s when I hit my problem…I wasn’t aware, until I tried it out, that you couldn’t use the IP + port number as an entry in the Hostfile. Using the IP on its own is fine but it didn’t like me adding the port number too! 😦

This meant that my amazing idea of showing this in action on Postman was ruined….or was it?! I headed to Google, that’s what we all do right – Thankfully, It didn’t take long till I found a comment on Stackoverflow, that mentioned that you could just use the HOSTS feature on Fiddler to do this instead. Fiddler is a free web debugging proxy tool and is an absolute must have for Developers and Testers working in the web development space. I use it all the time but because I never had a need to do it, I wasn’t even aware that you could do that within the tool.

To access this feature is simple. In Fiddler, select the Tools menu option and then select the HOSTS… option at the bottom of the list. I added the following entries and hit Save.

Fiddler_HOSTS
Fiddler HOSTS feature

 

I spun up my two mock node APIs and bingo!! We were back in business!!

dev-env
Dev Environment
staging-env
Staging Environment

So that was it, Fiddler saved the day and I was able to add what I needed to the Postman example and learnt something new in the process! Win – Win!!

Please do check out my examples on Github if you’re interested in learning more about different ways that you can use Postman.

Thanks for reading!!