Introduction
As a part of the Postman Tutorial – End to End tutorials series, in this post, we will learn to send a PUT request in Postman for a REST API.
Topics we are going to learn in this post
- Step by step process of sending a PUT request in Postman tool.
- How to add authorization in Postman for a PUT request?
Prerequisite
- You should have a preferably native Postman app installed on your system.
Sample APIs for Testing
I will use below PUT API for this post to show the demo:-
Update Booking – Update a booking
What is a PUT HTTP method or PUT API?
An HTTP PUT method is used to primarily update the resource information but it also can be used to create a new resource (Depends on API development) if the requested resource is not available. If a PUT request is made to update a resource, it should return 200 (OK) and 204 (No Content) status codes. If a PUT request is made to create a new resource if the target resource is not available then it should return a status code 201(Created).
PUT is not a safe method as it performs data creation and modifications but it is idempotent as if we hit the same request again, it operates on the same existing resource. But note here that a PUT request can be made as non-idempotent as well.
The reason that a PUT request can be used to create a new resource because we need to send a complete payload to the server where the server replaces the existing state of the resource with the latest payload. In a PATCH request, we just send data that we want to update. So PATCH can not be used to create new resources.
Gather information to send a PUT request
Let’s explore and understand our example APIs. You must need to go throw with the API documentation. The API documentation contains all information like URI, headers, cookies, authorization, payload, status codes, etc.
URI – https://restful-booker.herokuapp.com/booking/{bookingID}
Since we need to update the existing booking details then we must need to pass the booking id in URI as a path parameter. You can refer Create Booking API here to get a booking id.
HTTP Method – PUT
Headers – Content-Type: application/json
Since we are passing a JSON payload, Content-Type will be application/json.
Authorization – Required
Since the above API requires authorization. Restful-booker API supports two types of authorization.
You can either generate a token using Create Token API and pass it as a cookie to request or use Basic authorization as “YWRtaW46cGFzc3dvcmQxMjM=”. We will cover basic authorization later so in this post we will use Create Token API and use that token.
Request Payload – Edit data as you wish but do not change key names or add/remove keys.
{ "firstname" : "James", "lastname" : "Brown", "totalprice" : 111, "depositpaid" : true, "bookingdates" : { "checkin" : "2018-01-01", "checkout" : "2019-01-01" }, "additionalneeds" : "Breakfast" }
Step by step process to send a PUT request in Postman
- Launch the Postman tool app.
- Open the “Create a new request” template.
- Select the “PUT” in the 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 the “Send” button.
- View the response body and other details.
Launch the Postman tool app
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:-
Open Create a new request template
There are many ways to open a request template in Postman. A simple way to press Ctrl+N and click on “Request”.
On click on “Request”, it will open a new section where we need to give Request name, description(optional), and a collection name (existing or new). A collection is a group of requests or as of now, you can understand a collection is just a folder where you are saving requests.
Let’s fill in the required details meaningfully and click on “Save”. Please note since we do not have any existing collection so first click on “+Create Collection” and give a meaningful collection name and click on the tick mark. Once saved you will see a collection will be created left side and request will be saved within that.
Select the “PUT” in the HTTP methods dropdown
To select the desired HTTP method just click on the dropdown shown below and select. Make sure you click on the “Save” button so that changes in a request will be saved in the collection.
Pass the request URI in the address bar of Postman
We need to pass a booking id in URI as below where 10 is a booking id.
https://restful-booker.herokuapp.com/booking/10
You can refer Create Booking API here to get a booking id or use any existing one. Booking id 10 is an existing booking id.
Pass request payload
Since we are interested in updating booking details of an existing booking id we need to pass update details as a JSON payload. We need to pass a similar payload which we used to create a booking with updated data.
To pass a JSON payload as a 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.
Add authorization if applicable
If we hit the request now we will get 403 – forbidden because this API requires authorization.
We will generate a token using Create Token API. Since we already know to hit a POST request in Postman it will not be a problem to understand the below request. Just keep in mind that username and password are constant and whatever shown below that is only used to generate token.
We need to pass the above token as a header where the header name will be “cookie” and the value will be “token= 9aedd9b4ba3aa8f ” as mentioned in the API documentation.
Add headers if applicable
We added a cookie header in the above step. We also need to set a content-type header. Postman tool is an intelligent tool and when we add a JSON body then Postman will add the required content-type automatically.
Click on the “Send” button
Click on the “Send” button and it will change to “Sending“. It means you need to wait until the response of API is received. Once the request is processed you will get a response below.
View the response body and other details
The Postman tool shows the response received in a Response section with status code, headers, time taken to get a response, and size of the response.
You can import the above example collection from here.
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
Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.
Many other topics you can navigate through the menu.
tried post and put ,it is not working
Hey What error you are getting?
Sir, you’ve saved a junior dev, Thanks. Greetings from Mexico!
this is not working anymore
Hi Hanan,
I have updated post with latest changes. You can go through it again.
Thanks, helpful content
thank you, this is really helpful 🙂
Can I pass multiple user ids that I want to update in a single URI? If so how?
Hi Amod,
Thanks for sharing the API testing articles in details. Keep it up
Sure.