Hitting POST Requests and chain 2 requests using Postman

Welcome you all to the brand new ‘API Testing’ blog series! In the previous blog, we had studied how to execute GET requests using Postman. In this tutorial, we will look at POST requests. We will also see how to chain GET and POST requests!! So let us begin!!! 

1. What you will Learn in this blog?

  • Hit POST request without parameters
  • Hit POST request with parameters
  • Save the POST Request
  • Generate random unique IDs
  • Capture static id and use it in another POST request
  • Generate random id and use it in another POST request

2.  Hit POST request without parameters

Let us now look at POST request. See below, we have /post method that we can append with URI

So, let us create a new request, select POST from dropdown, enter the URI and append it with /post as shown below

Since this is a POST request, we have to send some body (data) as part of this request. When we execute this request, the data will be posted(created) on the server viz a new record will get created on the server side.

To add the body, see below. Go to the ‘Body’ section, select ‘raw’ radio button and further select JSON from the dropdown

Add a very simple json body

Click Send.

The server sends the response in the form of json data output. The status code is 200

3. Hit POST request with parameters

Let us now add 2 parameters as part of our POST request

Click Send.

As expected, the 2 parameters are seen as arguments in the response body, see below

The complete response body is as shown below

4. Save the POST Request

Let us save this POST request by giving it some name and adding it to the same collection that we had created in our last blog

Our collection now has 3 requests (2 GET requests and a single POST request)

5. Generate random unique IDs

Let us see how to generate random unique IDs using the /uuid method. This method can be seen in the website as shown below

Copy the URI https://httpbin.org/uuid

Paste it as shown below (this is a GET request)

Click Send.

Notice below that a unique id is generated in the response body

Click ‘Send’ again.

Notice below that another unique id is generated and is different from the above id

This way, we can keep generating different unique random IDs.

6. Capture static id and use it in another POST request

Sometimes, we have a requirement to capture response of 1st request and send that response as an input to the 2nd request. 

Another similar requirement can be: to generate a random id (uuid), capture this id and send it as part of another POST request. The below diagram depicts this scenario

Let us first see how to capture the static id from one request and use it in another request.

Click ‘Tests’ section

Under ‘Tests’ section, we see a snippet ‘Set a global variable’. We can use this snippet to work with the variables

Click this function ‘Set a global variable’. The below code gets generated

In the above sample code, we can see 2 global variables: ‘variable_key’ and ‘variable_value’.

Since these are global variables, we can call and use them in other requests as well.

Let us suppose the value of the variable ‘variable_key’ is uuid

Let us hard code the value of the variable ‘variable_value’ as t123, see below

Click ‘Send’ to execute the GET request

Now we would like to send the above global ‘uuid’ variable in another POST request.

We have one POST request saved in our collection

Open this POST request. Make sure ‘Body’ and ‘raw’ section are selected as shown below

To capture the value of global ‘uuid’ variable (from our GET request), we need to mention the ‘uuid’ variable in double curly braces {{}} as shown below

So basically, we have created a variable ‘id’ as shown above and the value of this ‘id’ variable would be the value of ‘uuid’ variable that would be fetched from the GET request.

Click ‘Send’ to execute this POST request.

Notice the json response below. The value of ‘uuid’ variable (t123) gets fetched from the GET request  

7. Generate random id and use it in another POST request

In the above scenario, we captured the hard coded value of uuid from GET request and used it in another POST request.

Our actual requirement is to capture the random (dynamic) uuid from one request and use it in another request.

Recall that, below is the sample random uuid as part of json response

To capture this json response, we can use the below code snippet. We are defining a variable jsn_response and inside this variable we are capturing the json response at runtime. Notice that we have pm.response.json() method available

So we can write

So the variable jsn_response holds the entire json response, including the value of uuid that gets generated during runtime, example, ec7e14f4-ef6e-47c9-9ec0-63b6524d7a94.

Next, we will remove the hard coded value ‘t123’. Instead of that, we will write jsn_response.uuid

 So we have

Let us now hit the ‘Send’ button of this GET request. The random uuid gets generated

Let us now go to the POST request and hit the ‘Send’ button. Notice below that the value of ‘id’ is same as the value of uuid that was generated in the above screenshot

Go to the GET request and let us hit the ‘Send’ button one more time. Note down the value of uuid, see below

Go to the POST request and hit the ‘Send’ button.

Notice that the value of ‘id’ is same as the value of ‘uuid’ generated above

Save the requests

Let us drag ‘GET_uuid’ request above ‘POSTwithParams’, see below

So this is how we can chain the GET and POST requests.

In the next article, we will perform some Test validations in the POST request. We will also see how to handle cookies.

Thank you for reading!


You may also like

API Basics and Architecture

API Basics and Architecture
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}