HTTP Methods and Their Purposes
- Vipul Kumar
- Rest , API , Webdev
- December 8, 2024
Table of Contents
🔍 GET — Used to request data from a specified resource without altering it. It is considered safe and idempotent, meaning it can be repeated without causing any changes.
📤 POST — Sends data to a server to create or update a resource. It is not idempotent, as repeated requests can result in different outcomes.
🔄 PUT — Replaces all current representations of the target resource with the request payload. It is idempotent, meaning multiple identical requests have the same effect as a single request.
🗑️ DELETE — Removes the specified resource. It is idempotent, as repeating the request will not change the outcome after the resource is deleted.
🛠️ PATCH — Applies partial modifications to a resource. Unlike PUT, it does not replace the entire resource.
🔗 CONNECT — Establishes a tunnel to the server identified by the target resource, often used for SSL tunneling.
🔍 HEAD — Similar to GET, but it retrieves only the headers of a resource, not the body. It is safe and idempotent.
⚙️ OPTIONS — Describes the communication options for the target resource, allowing clients to determine the capabilities of a server.
🔁 TRACE — Performs a message loop-back test along the path to the target resource, useful for diagnostic purposes.
HTTP Method Characteristics
🔒 Safe Methods — GET, HEAD, OPTIONS, and TRACE are considered safe as they do not alter the state of the server.
🔄 Idempotent Methods — GET, PUT, DELETE, HEAD, OPTIONS, and TRACE are idempotent, meaning repeated requests yield the same result.
🗄️ Cacheable Methods — GET and HEAD are inherently cacheable, while POST and PATCH can be cacheable under certain conditions.
🔄 Non-idempotent Methods — POST and PATCH are not idempotent, as they can result in different outcomes with repeated requests.
🔍 Non-cacheable Methods — Methods like POST, PUT, DELETE, and PATCH are generally not cacheable unless specified otherwise.
Common Use Cases
📄 GET — Commonly used to retrieve web pages, images, and other resources without modifying them.
📝 POST — Used for submitting form data, uploading files, and creating new resources on a server.
🔄 PUT — Often used to update existing resources or upload files to a specific location.
🗑️ DELETE — Used to remove resources, such as deleting a user account or a file.
🛠️ PATCH — Employed for making partial updates to resources, such as changing a user’s email address.
HTTP in RESTful APIs
🌐 RESTful APIs — Use HTTP methods to perform CRUD operations on resources, aligning with REST principles.
🔍 GET in REST — Retrieves resource representations, often used for reading data in RESTful services.
📤 POST in REST — Creates new resources or triggers server-side operations, such as processing a payment.
🔄 PUT in REST — Updates existing resources, ensuring the entire resource is replaced with the request payload.
🛠️ PATCH in REST — Allows partial updates to resources, providing flexibility in modifying data.
Follow me on: LinkedIn | WhatsApp | Medium | Dev.to | Github