HTTP Methods and Theirs Use Cases

HTTP Methods and Theirs Use Cases

What is HTTP Method?

HTTP methods are used to indicate the actions an API Client would like to perform on a given resource. Each HTTP method maps to a specific operation, such as creating, reading, updating, or deleting a resource, and an HTTP method must be included with every request to an API.

HTTP methods enable API clients to perform CRUD actions on an API’s resources in standardized and predictable way.

The most commonly used HTTP methods are:

  1. GET:

The GET method is used to retrieve data on a server. Clients can use the GET method to access all of the resources of a given type, or they can use it to access a specific resource.

  • Fetching a webpage (e.g., homepage of a website).

  • Retrieving data from a REST API (e.g., GET /api/users to fetch all users).

  • Performing searches (e.g., GET /api/search?q=example).

Characteristics:

  • Idempotent (multiple requests return the same result).

  • Should not change server state.


  1. POST:

The POST method is used to create new resources. For instance, if the manager of an e-commerce store wanted to add a new product to the database, they would send a POST request to the /product endpoint. POST requests typically include a request body, which is where the client specifies the attributes of the resource to be created.

  • Submitting a form (e.g., registering a user).

  • Adding a new item to a database (e.g., POST /api/products).

  • Processing file uploads (e.g., uploading an image).

Characteristics:

  • Not idempotent (multiple requests can create duplicate resources).

  • May result in server state changes.


  1. PUT:

The PUT method is used to replace an existing resource with an updated version. This method works by replacing the entire resource with the data that is included in request’s body.

  • Updating user information (e.g., PUT /api/users/1 to update user with ID 1).

  • Replacing an existing resource (e.g., replacing a document).

Characteristics:

  • Idempotent (repeated requests have the same effect).

  • Requires the full representation of the resource in the request body.


  1. PATCH:

The PATCH method is used to update an existing resource. It is similar to PUT, except that PATCH enables clients to update specific properties on a resource—without overwriting the others.

  • Updating specific fields of a resource (e.g., PATCH /api/users/1 to update only the email of user 1).

  • Applying incremental changes to a document.

Characteristics:

  • Not necessarily idempotent, depending on implementation.

  • Used for partial updates rather than full replacement.


  1. DELETE

The DELETE method is used to remove data from a database. When a client sends a DELETE request, it is requesting that the resource at the specified URL be removed.

  • Removing a user account (e.g., DELETE /api/users/1 to delete user with ID 1).

  • Deleting a file from storage.

Characteristics:

  • Idempotent (repeated requests usually yield the same result).

  • Results in removal of data.


Thank You for reading this article..