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
![Image 1](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-1-5-300x210.jpg)
So, let us create a new request, select POST from dropdown, enter the URI and append it with /post as shown below
![Image 2](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-2-5-300x131.jpg)
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
![Image 3](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-3-5-300x66.jpg)
Add a very simple json body
![Image 4](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-4-5-300x180.jpg)
Click Send.
The server sends the response in the form of json data output. The status code is 200
![Image 5](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-5-5-300x115.jpg)
3. Hit POST request with parameters
Let us now add 2 parameters as part of our POST request
![Image 6](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-6-5-300x137.jpg)
Click Send.
As expected, the 2 parameters are seen as arguments in the response body, see below
![Image 7](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-7-5-300x193.jpg)
The complete response body is as shown below
![Image 8](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-8-5-300x243.jpg)
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
![Image 9](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-9-5-300x162.jpg)
Our collection now has 3 requests (2 GET requests and a single POST request)
![Image 10](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-10-5-300x147.jpg)
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
![Image 11](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-11-5-300x123.jpg)
Copy the URI https://httpbin.org/uuid
Paste it as shown below (this is a GET request)
![Image 12](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-12-5-300x48.jpg)
Click Send.
Notice below that a unique id is generated in the response body
![Image 13](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-13-5-300x93.jpg)
Click ‘Send’ again.
Notice below that another unique id is generated and is different from the above id
![Image 14](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-14-5-300x70.jpg)
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
![Image 15](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-15-5-300x55.jpg)
Let us first see how to capture the static id from one request and use it in another request.
Click ‘Tests’ section
![Image 16](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-16-4-300x59.jpg)
Under ‘Tests’ section, we see a snippet ‘Set a global variable’. We can use this snippet to work with the variables
![Image 17](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-17-3-300x99.jpg)
Click this function ‘Set a global variable’. The below code gets generated
![Image 18](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-18-3-300x65.jpg)
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
![Image 19](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-19-3-300x48.jpg)
Let us hard code the value of the variable ‘variable_value’ as t123, see below
![Image 20](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-20-2-300x49.jpg)
Click ‘Send’ to execute the GET request
![Image 21](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-21-2-300x180.jpg)
Now we would like to send the above global ‘uuid’ variable in another POST request.
We have one POST request saved in our collection
![Image 22](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-22-2-300x190.jpg)
Open this POST request. Make sure ‘Body’ and ‘raw’ section are selected as shown below
![Image 23](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-23-2-300x159.jpg)
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
![Image 24](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-24-2-300x270.jpg)
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
![Image 25](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-25-2-223x300.jpg)
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
![Image 26](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-26-2-300x192.jpg)
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
![Image 27](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-27-2-300x120.jpg)
So we can write
![Image 28](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-28-2-300x112.jpg)
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
![Image 29](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-29-2-300x123.jpg)
So we have
![Image 30](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-30-2-300x115.jpg)
Let us now hit the ‘Send’ button of this GET request. The random uuid gets generated
![Image 31](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-31-2-300x188.jpg)
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
![Image 32](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-32-1-300x197.jpg)
Go to the GET request and let us hit the ‘Send’ button one more time. Note down the value of uuid, see below
![Image 33](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-33-1-300x199.jpg)
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
![Image 34](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-34-1-300x178.jpg)
Save the requests
![Image 35](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-35-1-300x158.jpg)
Let us drag ‘GET_uuid’ request above ‘POSTwithParams’, see below
![Image 36](https://rahulshettyacademy.com/blog/wp-content/uploads/2022/06/Image-36-1-300x177.jpg)
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!