Skip to main content

Command Palette

Search for a command to run...

Activity 29: HTTP Methods

Published
•4 min read•View as Markdown

1. GET Method

Purpose: The GET method is used to retrieve data from a server. It is a read-only operation and does not modify any data on the server.

When to Use GET:

  • When you want to retrieve data without side effects (i.e., without changing the state of the server).

  • For fetching resources, such as retrieving user information, lists of items, or database records.

Example:

Retrieving a list of users from a REST API:

https://api.example.com/users

Retrieving a specific user by ID:

https://api.example.com/users/123

Characteristics:

  • Idempotent: Multiple identical GET requests have the same effect as a single request.

  • Caching: Responses to GET requests can be cached to improve performance.


2. POST Method

Purpose: The POST method is used to send data to a server to create a new resource. It often submits data that the server processes and stores.

When to Use POST:

  • When you need to create a new resource on the server.

  • When submitting data that modifies the state of the server (e.g., creating a new user, adding an item to a shopping cart).

Example:

Creating a new user in a REST API:

https://api.example.com/users
Content-Type: application/json

{
  "name": "John Doe",
  "email": "johndoe@example.com"
}

Use Cases:

  • User registration forms

  • Uploading files

  • Submitting comments or feedback

Characteristics:

  • Non-idempotent: Multiple identical POST requests can create multiple resources, each with a unique identifier.

3. PUT Method

Purpose: The PUT method is used to update an existing resource on a server. It replaces the entire resource with the new data provided in the request.

When to Use PUT:

  • When you want to update a specific resource completely.

  • When you have the entire representation of the resource and want to replace it.

Example:

Updating a user’s information:

 https://api.example.com/users/123
Content-Type: application/json

{
  "name": "Jane Doe",
  "email": "janedoe@example.com"
}

Characteristics:

  • Idempotent: Multiple identical PUT requests have the same effect as a single request (the resource will remain the same).

  • The client must send the complete resource representation.

Difference from POST:

  • POST creates a new resource, while PUT updates an existing resource.

4. DELETE Method

Purpose: The DELETE method is used to remove a resource from a server. It requests the server to delete a specific resource identified by a URL.

When to Use DELETE:

  • When you want to permanently remove a resource from the server.

Example:

Deleting a user by ID:

https://api.example.com/users/123

Characteristics:

  • Idempotent: Sending the same DELETE request multiple times will not change the outcome after the resource has been deleted.

  • Often returns a status code indicating success or failure (e.g., 204 No Content for successful deletion).


5. PATCH Method

Purpose: The PATCH method is used to apply partial updates to a resource. Unlike PUT, which requires the entire resource representation, PATCH only needs the fields to be updated.

When to Use PATCH:

  • When you want to update only a few fields of a resource instead of the entire resource.

  • For scenarios where sending the complete resource is unnecessary.

Example:

Partially updating a user's email:

https://api.example.com/users/123
Content-Type: application/json

{
  "email": "newemail@example.com"
}

Characteristics:

  • Non-idempotent: Although multiple identical PATCH requests may not change the outcome, they may have different results based on the server's state.

  • It allows for more efficient updates, especially for large resources.

Comparison with PUT:

  • PUT requires the full resource representation to replace it, while PATCH only requires the changes (partial updates).

Summary Table of HTTP Methods

MethodPurposeIdempotent?Use Cases
GETRetrieve data from the serverYesFetching resources, querying data
POSTSend data to create a new resourceNoUser registration, form submissions
PUTUpdate an existing resource completelyYesUpdating user details
DELETERemove a resource from the serverYesDeleting users or records
PATCHApply partial updates to a resourceNoUpdating specific fields of a resource

More from this blog

Joshua Nato's team blog

56 posts