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

Ain’t Nobody Got Time for That…

ANGTFT

Sometimes you do certain tasks that become normal and you kind of go into autopilot, blindly repeating the same thing over and over again. It normally takes someone else or something else to spark something in your mind and give you an idea that there is always a simpler solution.

My spark came in the form of Alan Richardson, I’ve been a huge fan of his work for a while and I love the content that he creates and the weekly YouTube videos! A couple of weeks ago he released a short video about creating a bookmarklet to change some static text URL links on a web page, into actual clickable links, that open up a specific web page, a Twitter profile in this case. I thought that was really cool and looked like something that I could have a go at myself. All of the JavaScript code for doing this was written within the Chrome DevTools console – Perfect, I’m familiar with these already!

Recently, I’ve been using a strategy when testing, that involves creating “Test Queues” within RabbitMQ (The message broker that we use for our microservices) that siphon all the messages from the queues that our microservices are consuming – The Test Queues hold/store them, so that I can investigate deeper into the message data. Unlike the actual queues, these do not have consumers so the messages will stay there until I choose to delete them, adding an extra layer of control. I could just manually stop the service and grab the messages from the service queues but I like to give myself a separate option.

Screenshot from 2017-06-03 11-13-34
RabbitMQ – Publisher > Exchange > Queues > Consumers…

For context – My Test Queues would live alongside the red ones in the image above but will not have consumers, the arrows to the right of the red queues…hopefully, that makes sense. 🙂

My boring repetitive problem…

RabbitMQ is an awesome message broker and like it says on the site “Messaging that just works” what they haven’t concentrated on is the usability of their UI management console, why should they, that’s not their main focus – they currently just have something that’s good for now. As much as having these queues is really useful, clearing them out or purging them is a tedious task, it’s made worse by the fact that I follow the same process several times a day…

Click on the queue name > scroll to the bottom of the page > Click on the “Purge” button > scroll back to the top of the page > Select the Queue tab to get back to the main view…repeat…yawn!

I’m a fan of getting a local instance of the technologies that we use within our feature team and exploring lots of different aspects so I feel more comfortable and more informed about the tools I’m working within. I knew the RabbitMQ has a HTTP Restful API with a limited amount of features, I can use some of these to my advantage!!

RabbitMQ has been around for about 10 years now so I had a hunch that someone has probably had the same problem as me and wanted to use the API to purge the messages within a queue…I headed over to Google…

Basically, the first result that came back was about 90% of the solution that I required…Bingo! I found this from 5 years ago – Like I said, it wasn’t exactly what I wanted so I needed to adapt the code to suit my requirement.

I grabbed the code and pasted it into a new Snippet within the “Sources” tab on the Dev Tools, I love this feature, it’s like a little playground for practising and running basic JavaScript. The code in the gist deletes the messages from every single queue which was the main part that I needed to change, I tend to apply a simple naming convention to my queues and prefix them all with “TestQueue_” – This is just so that they are all grouped together within the management console.

TestQueues_RabbitMQ
My Test Queues with lots of lovely messages…

I only needed to refactor the code slightly and add an IF statement that checks to see if the queue name starts with the “TestQueue_” name if so, purge the queue. All the other queues are unaffected. Job Done!

RabbitManagementConsole
Slightly refactored code with an IF Statement…

During the testing of the code, I added some logging just to sanity check that I was getting the correct queues that I wanted and discarding all the other ones. All the “null” entries below are queues that do not start with “TestQueue_” so are ignored.

Getting_Queue_Names
Iterating over all the queues to get the ones I want…

Once the Snippet was run, I could see that the correct requests are being made…Yay! The messages were deleted from only the queues I wanted.

Console_Output
Console Output showing the DELETE requests…

I have created a gist of the code that I used to Purge the Queues – https://gist.github.com/DannyDainton/2bae06e3ca898440cdc0452a727ee7bf

I could have just settled for that solution and ran this script from the Dev Tools each time but I wanted it to be even easier for me (I’m very lazy) so that’s where Alan’s bookmarklet app comes in handy, you can take any piece of JavaScript code that you have coded to do something on a page and it will create an encoded link that you can add to your browser bookmarks.

Bookmarklet App
Creating a new bookmarklet with my JavaScript code…

So I now have a link on my browser that saves me the pain of going through the tedious repetitive task to purge each queue. It’s the simple things in life that make me happy.

Alan wrote a post to accompany his video that references lots of links about how others have used bookmarklets. One of those is this excellent blog post by Abby Bangser, which I read a while ago before I saw the YouTube video. She explains how she utilizes the bookmarklets to quickly fill in form data – It’s really interesting and I would recommend trying to give it a go yourself.

Hopefully, that has been interesting enough to spark something in your own mind and attempt to give this a go – I would love to hear from anyone who has created a bookmarklet to solve a problem.

Cheers!

30 Second APIs

In the UK we have a chef called Jamie Oliver and over the years he has created lots of TV episodes and also a book on how to make a complete family meal in 30 Minutes – The premise of this, is that time doesn’t have to be an excuse not to make healthy food for the family.

30_Min_Meals

Now, if you’ve ever seen these episodes or tried to follow this, it takes a lot longer than 30 minutes!! It’s more about a “mindset”, which is a lie, it’s about having everything prepped and ready to go so all you’re doing is cooking the food and not chopping things up and getting the pots and pans ready.

So what does this have to do with APIs?!

In my current context, making requests to a RESTful API and analysing the response data is something I’m perfectly comfortable with doing but that’s only because I’ve been working within that space for a couple of years now. I truly take for granted many of the skills that I’ve learnt over the last couple of years and I forget, that you need to start somewhere, in order to build up your knowledge of a certain area.

There are loads of public APIs out there that you can use but these can be tricky to set up. In my experience, most of the documentation that comes with these is confusing and sometimes it’s lacking vital information. Another way forward is to create your own basic API but this can be a daunting task coding it yourself, although a very rewarding once you get it working!

What if there was an even easier way that would allow you to have control over the requests that you make and the data that you want to see in the response?!

I’ve found this node module called json-server that is very handy and super quick to get you started. It’s only a couple of commands from a terminal and you are up and running and making requests within seconds….seriously!

The 30 second API mindset…

So following Jamie’s lead, before starting we need to do our Mise en place…We require a couple of things in place before we get cooking.

Prep Work:

  • Download and Install Node.js
  • Download and Install Postman
  • Create a new Folder on your machine
  • Open a Terminal in the new directory

Your starting point should look something like this – depending on your terminal/Postman colour preferences…
Starting_Point

First of all, we need to globally install the json-server module. In the open terminal, type the following command:

npm install -g json-server

Once this has been installed you’re basically ready to make requests. Quick right?! As you have installed the module globally (using the -g flag), the server can be started from any directory but we will be using our newly created one.

In the terminal, type:

 json-server db.json

json_server_db_file
As you do not have a db.json file in that directory, it will conveniently create one for you and populate this with some default values. These are good for now, just to get you started and making basic requests. Later in the post, we will talk through how to create data files containing the data that is more relevant to your context.

We’ll be using Postman to request the data from the db.json file but it’s entirely up to you, use something that you feel comfortable using. You can now send a request and get a response containing data from the file. The available routes are displayed in the terminal, you can use either of these routes in your first request.

Copy one of the routes below and paste this straight into Postman, you will get the data returned from that Endpoint.

 

GET_Request

Sending a GET request is easy right, the best thing about the json-server module is that it gives you the ability to also practice sending POSTs, PUTs and DELETEs. I would attempt go through each one of these methods but the documentation on the json-server GitHub page explains these methods better than I ever could. This also covers a number of awesome features that you can make use of when using the application.

I don’t want to use the Default data file…

Fear not, as this is essentially just a JSON file that you’re using, you can have any data you desire. You’ll need to stay within the applications format for the way it parses the routes/endpoints from the .json file but apart from that, it’s up to you!

I’ve used mockaroo to create some random data. You can grab this from here and save the .json file to the same directory. Stop the current server instance (Ctrl+C) and spin up a new one using the new dummyUsers.json file:

json-server dummyUsers.json

Requests can now be made to access the new data – Try a GET request to http://localhost:3000/users or how about selecting just one user http://localhost:7000/users/{id}

Request_Single_User

Handy Extras…

If like me, you have applications running on certain ports on your local machine, you will need to either stop that process or give json-server a different port to use. This can be done from the command line before you start the server:

json-server <file.json> -p <port number>

When making changes to the .json file manually in a text editor you will need to stop and start the server instance that you have running, for the changes to take effect, which obviously sucks because you want to be able to see your changes straight away. Luckily, there’s a command line flag to get over this:

json-server –watch <file.json>

Any manual file changes made will be automatically detected and the server will be restarted for you and you’ll be able to continue making those lovely requests.

To Finish Off…

So that’s it, you now have a platform to practice making requests to an API that you have control over and all without writing any code. I’ve only covered a small part of what json-server offers during this post, I would advise you to have a look through the GitHub page once you’re comfortable using the basic function of the application because it offers so much more.

Good luck and let me know if you find this useful. If you have any questions just drop me a message!!

An Extra Helping…

I’ve added some more information about using a collection within Postman to make different types of requests and interact with the dummyUsers.json data. Hope it helps!!

https://github.com/DannyDainton/API-Postman-Practice