This tutorial aims to create a PUT request in Postman and demonstrate how to execute it.
Table of Contents
What is a PUT Request?
A PUT request is an HTTP method used to update or replace a resource on a server. When a client sends a PUT request, it typically includes the updated data for the resource in the request body. The server processes this request by replacing the current representation of the resource with the new data provided by the client. If the resource does not exist, some implementations may create it.
- Characteristics of a PUT Request
- Idempotence: PUT requests are idempotent, meaning that multiple identical requests should have the same effect as a single request. For example, if you send the same PUT request multiple times, the resource's state will remain the same after the first update.
- Resource Identification: The URL in the PUT request specifies the resource to be updated. The server uses this URL to locate the resource and update it with the data from the request body.
- Full Update: PUT requests generally replace the entire resource with the data provided. A PATCH request might be more appropriate if you only want to update part of a resource.
- PUT Request Structure
- A typical PUT request includes:
- HTTP Method: PUT
- URL: The endpoint where the resource resides
- Headers: Metadata for the request, such as Content-Type
- Body: The new representation of the resource
Difference between POST and PUT
Here are some differences between POST and PUT HTTP methods:
- Purpose
- POST: Used to create a new resource or submit data to the server for processing.
- PUT: Used to update or replace an existing resource.
- Idempotence
- POST: Not idempotent. Multiple identical POST requests can create multiple resources.
- PUT: Idempotent. Multiple identical PUT requests have the same effect as a single request.
- Resource URI
- POST: The server decides the URI of the new resource.
- PUT: The client specifies the URI of the resource to be updated or created.
- Creating vs. Updating
- POST: Primarily used for creating resources.
- PUT: Primarily used for updating or completely replacing resources.
- Partial vs. Full Update
- POST: This can be used for partial updates or for operations other than resource creation.
- PUT: Typically replaces the entire resource with the data provided.
- Request Payload
- POST: The request payload contains the data to be processed, which may result in the creation of a new resource.
- PUT: The request payload contains the data to replace the existing resource.
- Response Status Codes
- POST: Common status codes include 201 (Created), 200 (OK), or 202 (Accepted).
- PUT: Common status codes include 200 (OK), 204 (No Content), or 201 (Created) if the resource is newly created.
- Server Action
- POST: The server processes the request payload to perform an action, such as creating a new resource or processing form data.
- PUT: The server updates the specified resource with the new data provided in the request payload.
- Cacheability
- POST: Responses to POST requests are generally not cacheable.
- PUT: Responses to PUT requests can be cacheable if indicated by the response headers.
- Usage in RESTful APIs
- POST: Often used for actions like form submissions, creating new records, and other operations where the client does not know the resource URI.
- PUT: Used for updating existing records where the client knows the resource URI and can replace the resource with the provided data.
Request URL
https://restful-booker.herokuapp.com/booking/bookingid
Sample Request Body{
"firstname": "Testing",
"lastname": "Therapy",
"totalprice": 2250,
"depositpaid": true,
"bookingdates": {
"checkin": "2024-05-30",
"checkout": "2024-06-01"
},
"additionalneeds": "Dinner"
}
Implementation Steps
Make a collection
Please follow this post to learn how to create a Collection in Postman
Make a request to the Collection
Step 1: To create a new request, click on the 3 dots and select “Add request”.
Step 2: Once you create a new request, then you will get the following window:Step 1: Enter the “name” in the request. Here, the name is “Update Booking”.
Step 2: Enter the “URL” in the address bar. Add the
"bookingid" after
"booking/" in the URL. You will get this
"bookingid" from the
Create Booking request.
Step 3: Now, select the “PUT” request from the list of request methods.
Step 4: Add a Request Body to the Post request Steps:
Select the JSON
Add a Request body
This is done to ensure that we send the request in the correct format that the server expects. The request body example mentioned at the beginning of the tutorial should be copied and pasted into the Postman request body.
Step 5:: Press the “Send” button.
Response
Now, it is showing that an access token is required. The HTTP status code '403 Forbidden—You don't have permission to access this resource' is displayed when a web server recognizes a user's request but cannot allow additional access.
Please follow this post to learn how to generate an access token.
Step 6:: Add 'Access Token' in this request
Click on the 'Headers'
Set the Query Params key as Cookie and set the Value to 'token=token value'
Press the “Send” button.
Ensure that the Response has been validated
Format Type
Each request has a defined response to it as defined by the Content-Type header. That response can be in any format. Such as in the above example, we have a JSON code file.
Below are the various format types present in Postman.
JSON
Congratulations on finishing this tutorial and I hope you found it helpful! Happy learning!