Postman Tutorial Part 5 – Sending POST Request in Postman
As a part of Postman Tutorial – End to End, in this post, we will learn “How to send POST request in the Postman tool.“
- You should have a preferably native Postman app installed on your system.
- Restful-booker API
- GoRest API
I will use below POST APIs for this post to show the demo:-
- Create Booking – Creates a new booking
- Create User – Create a new user
Let’s explore and understand our example APIs.
URI – https://restful-booker.herokuapp.com/booking
HTTP Method – POST
Headers – Content-Type: application/json
Authorization – Not required
Request Payload – Edit data as you wish but do not change key name or add/remove keys.
{ "firstname" : "Jim", "lastname" : "Brown", "totalprice" : 111, "depositpaid" : true, "bookingdates" : { "checkin" : "2018-01-01", "checkout" : "2019-01-01" }, "additionalneeds" : "Breakfast" }
URI – https://gorest.co.in/public-api/users/
HTTP Method – POST
Headers – Content-Type: application/json
Authorization – Bearer – Access Token
To get an access token you need to login to GoRest. Once you login using GMail or Facebook, you can get your access token.
Request Payload –
{ "name": "Tenali Ramakrishna", "gender": "Male", "email": "[email protected]", "status": "Active" }
An HTTP POST method is used to create a new resource in the collection of resources with a request body passed as a JSON/XML or in a different format. If a resource is created successfully by the endpoint or server, it returns a status code 201( Created) (Not always) and returns the response body. It may return 200 (OK) and 204 (No Content) status code as well based on how API is developed.
POST is not a safe method as it is related to data creation. It is also not idempotent and invoking two identical POST requests may result in two different resources containing the same information with just different resource ids. An API may have a check on duplicate records as well like email id or mobile number is not unique. In that case, a POST request may behave as idempotent.
A request body contains data that is used to create a new resource. For example – You want to register a user on some portal and you need to pass your name, age, and address. Here name, age, and address are parameters/keys that hold a value in a request body.
- Launch the Postman tool app.
- Open “Create a new request” template.
- Select the “POST” in HTTP methods dropdown.
- Pass the request URI in the address bar of Postman.
- Pass request payload
- Add authorization if applicable.
- Add headers if applicable.
- Click on “Send” button.
- View the response body and other details.
I will demo Create Booking API followed by Create User API.
Launch the installed Postman tool. If you have not installed the Postman tool then you can install it from here. Please note here that I have created an account on Postman. If you have not created then you will be seeing some slightly different screen from below:-
There are many ways to open a request template in Postman. You can use any option shown below:-
A request template will be shown as below:-
Since a POST request is responsible to create a new resource, it requires data to work on. A POST API may ask for data in multiple ways. Our both example APIs take a JSON payload as a body.
To pass a JSON payload as body follow the below steps:-
- Click on “Body” tab.
- Select “raw” radio button.
- Select “JSON” from the last dropdown.
- Copy-paste JSON body in the body text area.
Create Booking API does not require any authentication. So I will skip this step. But for Create User API we require authentication which we will see after sometimes.
Headers represent the metadata of API requests and responses. A header is a key-value pair. Our example APIs accept a request payload as JSON which we need to let server know explicitly by adding a header named (key) “Content-Type” with value as “application/json”. All required header information will be provided in API doc by the developer. If it is not provided you need to ask them.
To add a header, follow the below steps:-
- Click on “Headers” tab.
- Enter header name as key in a row.
- Enter header value as value in corresponding row.
Postman will start giving suggestions while typing header names and values.
I have added only one header but it shows count as 10. Actually, Postman adds some default headers automatically which are hidden by default. You can see those by clicking on “9 hidden” which you can see in the above image.
Click on “Send” button and it will change to “Sending“. It means you need to wait until the response of API is received. Once request is processed you will get response below.
The Postman tool shows the response received in a Response section with status code, time taken to get a response, and size of the response.
For Create User API, follow the same steps as above. For “Authorization” step, we need to have an extra setup for this API as it requires authorization.
Details of the authentication mechanism of an API are provided in API doc. GoRest API’s authentication mechanism is provided on their website as below:-
There are three ways to pass authentication. We will Oauth2. Follow the steps as below:-
- Click on “Authorization” tab.
- Select “OAuth 2.0” in “Type” dropdown.
- Get the access token after creating an account on GoRest as shown above and paste in “Access Token” text box.
Note – I have just hide my access token by using stars(*).
Response is as below once you hit “Send” button.
You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.
If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading
#HappyLearning