DEV Community

Cover image for Understanding HTTP Methods
Sreya Satheesh
Sreya Satheesh

Posted on • Updated on

Understanding HTTP Methods

HTTP (Hypertext Transfer Protocol) methods are ways to tell a server what you want to do with a resource. Each method has a specific purpose, and you choose one based on what you need to do. The most commonly used HTTP methods are:

  1. GET
  2. POST
  3. PUT
  4. DELETE
  5. PATCH

GET

The GET method requests data from a specified resource.

Example: Imagine you're browsing an online bookstore. When you click on a book's title, your browser sends a GET request to the bookstore's server asking for the details of that specific book. The server then responds by sending back the book's title, author, description, and other details. GET requests are like asking for information from a server, such as fetching a webpage or an image.

POST

The POST method is used to submit data to be processed to a specified resource.

Example: Suppose you're filling out a registration form on a website. When you click the 'Submit' button, your browser sends a POST request to the server with all the information you entered (like your name, email, and password). The server then takes that data, processes it, and creates a new account for you. POST requests are used when you want to send data to the server to create or update something.

PUT

The PUT method is used to update a resource or create a new resource if it does not already exist.

Example: Let's say you want to update your profile picture on a social media platform. You select a new picture and click 'Save'. Your browser then sends a PUT request to the server with the updated picture file. The server receives this request, replaces your old profile picture with the new one, and updates the database. PUT requests are used when you want to completely replace an existing resource with new data.

DELETE

The DELETE method deletes the specified resource. It is used to remove resources from the server.

Example: Imagine you want to delete a comment you made on a blog post. When you click 'Delete', your browser sends a DELETE request to the server with the ID of your comment. The server then finds and removes that comment from its database. DELETE requests are used when you want to remove a resource from the server.

PATCH

The PATCH method is used to apply partial modifications to a resource.
It sends data to modify an existing resource, but unlike PUT, it applies partial updates rather than replacing the entire resource.

Example: Suppose you want to edit the text of a blog post you wrote. You make your changes and click 'Update'. Your browser sends a PATCH request to the server with just the edited parts of the blog post (like the new text paragraphs). The server then applies these changes to the existing blog post without altering the rest of the content. PATCH requests are used when you want to make partial updates to a resource.

Less Commonly Used HTTP Methods

In addition to the main HTTP methods listed above, there are a few other methods that serve specific purposes:

HEAD: Similar to GET, but only retrieves the headers of the response, not the actual content. It is useful for obtaining meta-information about resources without transferring the entire representation.

OPTIONS: This method is used to describe the communication options for the target resource. It allows the client to determine the options and/or requirements associated with a resource before making a request.

TRACE: This method echoes the received request so that a client can see what (if any) changes or additions have been made by intermediate servers.

CONNECT: This method establishes a tunnel to the server identified by the target resource.

Top comments (0)