Chaining Requests in Postman — Part 1

Asheesh Misra
10 min readJan 19, 2019

This post is for readers who understand basics of Postman and want to enhance their learning further. We will be learning how to chain requests to serve our purpose (i.e. testing). Also, we will see how ‘pre — request scripts’ can come in handy. Let’s get going!

Part 2 of this post can be found here.

We will be using Imgur API. Readers can sign up for the API here. After signing up, navigate to the Imgur API docs hyperlink and setup Postman using the instructions given on that page. If you configure everything as described there, you should have a valid access token to use for your API request to Imgur.

A collection in Postman may be understood as a named group of requests. Usually, similar API requests are grouped in a collection. The requests in a collection may be arranged in a certain order (of execution) and can be run individually or via the collection runner in Postman.

Let’s create a collection ‘Imgur API — Request Chaining’, to hold our requests. Open Postman native application on your system. Close the startup screen and then click on the ‘New’ button and selection ‘Collection’ option from the menu.

Alternatively, you may click on the ‘New Collection’ button on the ‘Collections’ tab in the ‘Side Panel’.

Enter ‘Imgur API — Request Chaining’ in the ‘Name’ textbox on the ‘Create A New Collection’ popup and click on ‘Create’ button (You may optionally type in ‘description’ for your collection). Observe that a new collection with the name you specified above is visible in Side Panel and a new request (with default GET https verb) has been been loaded in the application workspace.

Let’s specify the Imgur API endpoint to fetch user details:

https://api.imgur.com/3/account/asheeshmisra

For this endpoint to work, we need to specify a valid access token (the one that was obtained while signing up on Imgur and registering an application by following the steps mentioned here. In fact, to access any endpoint of the Imgur API, we need to supply a valid access token. Doing so for each request, however, is cumbersome and error-prone. Thankfully, Postman provides us environment variables to save the day.

Let’s create create an environment first. In Postman, click on the gear icon visible in the top right corner of workspace.

Click the ‘Add’ button on the ‘Manage Environments popup thus loaded, specify environment’s name and click ‘Add’ button again to save the environment.

After the environment has been created, it should be displayed with other environments (if any) that you have created, in the ,Manage Environments’ popup. You may edit it later if needed by clicking its name and can even delete if by clicking on the ellipsis (…) and then selecting ‘Delete’. We will edit/ view the environment a bit later.

Now that the environment is ready, let’s create an environment variable. Open the ‘Manage Environments’ popup by clicking on the gear icon in top right corner of Postman application workspace and then click on the name of the newly created environment. The ‘Manage Environments’ popup should now show the same UI again which was displayed when the environment was created.

Let’s specify ‘access_token’ in the variable textbox and its value in the ‘Initial Value’ textbox and click on ‘Update’ button.

Now that the environment variable has been created, let’s use it. To use an environment variable in the request, it needs to be enclosed in “{{}}”. To do so, let’s first create a GET request to get user details from Imgur API. Right click on the newly created collection and select the ‘Add Request’ option from the popup menu. Then specify ‘GETting User Details’ in the ‘Request Name’ textbox of ‘Save Request’ popup. Observe that the name of the collection you created is being displayed in the ‘Select a collection or folder to save to’ section. Click on ‘Save to…’ button to save the request.

Double click on the request thus created to load in the application workspace. Enter the following API endpoint in the request

https://api.imgur.com/3/account/asheeshmisra

Obviously, instead of my username it would be yours.

Now click on the ‘Headers’ tab of the ‘Request’ section, specify ‘Authorization’ under the ‘Key’ column and {{access_token}} under the ‘Value’ column. Then click on ‘Save’ button and then click on ‘Send’ button.

We expected that since we created an environment variable for our access token and have pass the token (as an environment variable) with the request, we should have got the user’s details BUT we got 403 status code (Permission Denied). Why? Because the environment token was not initialized to the environment before the request was made. To resolve this problem, we use something called the ‘Pre-Request Script’. From the postman’s official documentation,

Pre-request scripts are snippets of code associated with a collection request that are executed before the request is sent.

In the context of our problem pre-request script can be used to set the environment variable for the access token, before it is used in the script.

One more important thing to note here is the execution order in a request. So, a pre-request script associated with the Request will be executed first. Then the Request itself, then, after the Response has been received, the test script would be executed.

Back to our Postman App, click on the ‘Pre-request Script’ tab in the ‘Request’ section and then click on the ‘Set an environment variable’ link under the ‘Snippets’. You should see a line of code embedded in the ‘Pre-request Script’ tab, now. Edit this line by changing “variable_key” to “access_token” and “variable_value” to “Bearer <our access token>” and save the request.

Now, click on the ‘Send’ button to send the request. This time you should have success with a proper JSON response and Status code of 200, Yay!. Now that we have successfully got ourselves authenticated (and authorized), let’s upload an image to Imgur. To do so, we need to create a new request of ‘POST’ type, in our collection, URL to upload image and parameters to be passed along with the request.

We can easily create a new request in your collection by right clicking on the Collection’s name, specify request’s name, say, ‘Upload Photo Test’ on the ‘Save Request’ popup and click on ‘Save to <Our Collection Name>’ button. Observe that the newly created request is of ‘GET’ type and is being displayed under your Collection in the Side Panel. Click on it to load it in the application workspace and then click on the request type dropdown adjacent to the textbox wherein we would specify the request URL. From the dropdown thus loaded, select ‘POST’.

Next, we need the Imgur URL to post our request. So, head over to Imgur API Documentation and click on ‘Image’ -> ‘Post Image Upload’. Copy the ‘Post Image Upload’ url from there and paste it in the ‘request URL’ textbox of our POST request, in Postman.

Also, note that only ‘image’ parameter is ‘required’. To avoid in copyright infringement issue, we would use a free stock photo. To get a free stock photo from the Internet, I use Pexels for my purpose, but you can google other options as well. After downloading a free stock photo to a folder on your local system, click on the ‘Headers’ tab of ‘Request’ section of your ‘Post’ request. Specify ‘Authorization’ as key and ‘{{access_token}}’ as the value. BUT, as per the Imgur API documentation for the post request, we need to pass the client_id, also.

Recollect that in the response we got from our ‘GET’ request, ‘client_id’ was also present. To extract that from the response and use it in our POST request, we will set it as an environment variable in the test script for the ‘GET’ request. Type the lines of code as shown in the image below and save changes.

For the environment variables to be initialized, we need to run the GET request again. After re running the GET request, click on the ‘eye’ icon adjacent to the Environment dropdown and observe that our environment now has three variables, access_token, clientId and url, their corresponding values. It can be confirmed further that the values for ‘clientId’ and ‘url’ are the same as in the response body of the GET request.

Navigate to the ‘POST’ request now, and in the ‘Headers’ tab of the ‘Request’ section, add another key ‘Authorization’ with value as ‘Client-ID {{clientId}}’. Observe that the color of ‘{{clientId}}’ is orange which indicates that it has been initialized (if it had not been initialized, its colour would have been red)

Click on the ‘Body’ tab of the ‘Request’ section of the ‘POST’ request. Specify key value pairs as shown in the image below. Please remember that only ‘image’ key is ‘required’, remaining are optional. When we hover on the ‘image’ key we should see a dropdown to select the content type for this key. Select ‘File’ from that dropdown and we should see a ‘Choose Files’ button under the ‘Value’ column. Click on the ‘Choose Files’ button and select the free stock photo we wish to upload.

Now, let’s write a few tests to confirm successful upload of the image.

pm.test(“Response time is less than 20000ms”, function () {

pm.expect(pm.response.responseTime).to.be.below(20000);

});

pm.test(“Status code is 200”, function () {

pm.response.to.have.status(200);

});

var responseData=pm.response.json();

pm.test(“Upload was successful”,function(){

pm.expect(responseData.success).to.eql(true);

});

The tests that have been written confirm that:

  • Request has a response time of less than 20s (I know it is quite a high value but this on my slow broadband connection at home, you may reduce it depending upon your internet connection’s speed).
  • Status code is 200.
  • Upload was successful.

We have also set an environment variable ‘linkToPost’ that we will use in last request of our collection (and this part of tutorial).

Now that we have headers, body and test script ready for our POST request, let’s run the POST request to upload the photo by clicking the ‘Send’ button.

As is visible from the screenshot below, one test (for the response time) failed and rest tests passed. Also, it we view our environment variables, we would find that we have new variable ‘linkToPost’ added to our environment. We will use this variable in our next request.

Now that the file has been uploaded, create a new request (as described above), with the name “Confirm the image upload” and let it be of ‘GET’ type only. In the request url for this request, enter ‘{{linkToPost}}’, save changes and click ‘Send’ button. Observe that the ‘Body’ tab of ‘Response’ section actually displays the image we uploaded. This confirms that the upload was successful.

Food for Thought

So, we learnt how information from one request can be used in another request, i.e, how the requests can be chained together to actually test a bigger use case. We also learnt how environment variables and pre — request scripts can help us in chaining the requests.

However, there is a catch in what we learnt and that is, whatever we did above was all running one request at a time. What if we wish to run all the request one after the other? Of course, that can be done by running the collection itself. BUT, there is a problem in doing so. We explicitly specified the file to be uploaded in the POST request. When the collection runs there is no means to do so.

This problem is a bit tricky and can be solved using Newman. We will learn how to do that in the part 2 of this tutorial.

If you have any suggestions for this tutorial, please leave them in the comments.

--

--

Asheesh Misra

ISTQB Certified Tester @Metacube, Jaipur| does Automation, Performance, API Testing| listens spiritual discourses and good music| continuous learner