DEV Community

Cover image for Most Common HTTP Methods: A Simple Guide
E
E

Posted on

Most Common HTTP Methods: A Simple Guide

Introduction

If you've ever worked in web development, you've probably heard of HTTP methods. They're the building blocks of communication between clients (like browsers) and servers. But what do they really mean, and when should you use each one? In this article, we'll dive into the most common HTTP methods - GET, POST, PUT, DELETE, and PATCH - and break down their purpose and use cases with examples that will make everything crystal clear. Whether you're a beginner or just looking to refresh your memory, this guide is for you!

Common HTTP Methods

1. GET: Retrieving Data

Use Case: The GET method is the most commonly used. When you type a URL into your browser or click a link, it sends a GET request to retrieve data from the server. It's like saying, "Hey server, give me this information."

Example: Imagine you have an e-commerce site, and you want to show all products to the user. A GET request might look like this:

GET /products
Enter fullscreen mode Exit fullscreen mode

The server responds by sending back a list of products. Easy, right?

Key Point: GET requests are read-only. They should not change anything on the server; they are just for fetching data.

2. POST: Sending Data to Create Something

Use Case: The POST method is used when you want to send data to the server to create a new resource. It's like submitting a form with information that you want the server to store.
Example: Let's say a user fills out a registration form to sign up. When they click "Sign Up," a POST request might be sent:

POST /users
Enter fullscreen mode Exit fullscreen mode

This request includes the user's details (like their name and email) in the body of the request. The server processes this data and creates a new user account.

Key Point: POST requests are not cacheable, and they often change the state of the server by adding new data. It's important to use them when you want to create something new.

3. PUT: Updating Existing Data

Use Case: The PUT method is for updating existing resources. It's like saying,

"Hey server, here's the new version of this thing. Replace the old one."

Example: Imagine you want to update the details of a product in your e-commerce store. A PUT request could look like this:

PUT /products/123
Enter fullscreen mode Exit fullscreen mode

Here, /products/123 represents the specific product you want to update. The request would include all the updated product information (like price or description) in its body.

Key Point: PUT is idempotent, meaning if you send the same request multiple times, it should have the same effect as sending it once. This is different from POST, which may create multiple resources if sent repeatedly.

4. DELETE: Removing Data

Use Case: The DELETE method, as the name suggests, is used to delete resources from the server. It's like telling the server, "Remove this item."
Example: If a user wants to delete their account, a DELETE request could be made:

DELETE /users/123
Enter fullscreen mode Exit fullscreen mode

This tells the server to remove the user with the ID 123. After the request, the server should confirm that the user is gone.

Key Point: Like PUT, DELETE is also idempotent. No matter how many times you call it, it should result in the resource being deleted (if it exists).

5. PATCH: Partially Updating Data

Use Case: PATCH is similar to PUT, but it's used for partial updates. Instead of replacing the entire resource, it only updates the fields you specify.

Example: Say you want to update just the price of a product, not its entire information. You could send a PATCH request like this:

PATCH /products/123
Enter fullscreen mode Exit fullscreen mode

In the request body, you would include only the fields you want to update, like:

{
 "price": "19.99"
}
Enter fullscreen mode Exit fullscreen mode

The server then updates just the price without touching the rest of the product details.

Key Point: PATCH is useful when you don't want to send the entire dataset again, especially if it's large. It's efficient and straightforward.

Other Uncommon HTTP Methods

While most web developers are familiar with the common HTTP methods like GET, POST, PUT, DELETE, and PATCH, there are several lesser-known methods that serve specialized purposes. Understanding these uncommon methods can be valuable when you need more granular control over HTTP communications.

1. HEAD

Use Case: The HEAD method is similar to GET but only retrieves the headers of a resource, not the body. It's often used to check if a resource exists or to inspect its metadata without downloading the entire content.

Example: If you want to check if a specific image is present on the server before attempting to fetch or display it, you can send a HEAD request:

HEAD /images/picture.png
Enter fullscreen mode Exit fullscreen mode

The server responds with the headers, including status information and metadata (like content type and length), without sending the image itself. It's a lightweight way to verify resources.

2. OPTIONS

Use Case: The OPTIONS method is used to determine the HTTP methods supported by the server for a specific URL. It's especially helpful when you're working with APIs and want to understand which actions are allowed.

Example: If you're not sure what methods an API endpoint supports, you can send an OPTIONS request:

OPTIONS /users
Enter fullscreen mode Exit fullscreen mode

The server responds with allowed methods like GET, POST, or PATCH for that endpoint. This is commonly used in the context of CORS (Cross-Origin Resource Sharing) to check what headers and methods are permitted.

3. TRACE

Use Case: The TRACE method is used for diagnostic purposes. It echoes back the received request, allowing the client to see what changes or modifications were made along the request path by intermediate servers.

Example: This method is rarely used in modern development because of potential security risks, such as Cross-Site Tracing (XST) attacks. However, it can be used to debug the path a request takes through proxy servers or firewalls.

TRACE /api/resource
Enter fullscreen mode Exit fullscreen mode

The server responds with the original request so you can trace its route.

4. CONNECT

Use Case: The CONNECT method establishes a tunnel to the server, commonly used for setting up SSL connections via HTTPS. It is a fundamental part of how proxy servers work to create secure connections.

Example: When you access a secure website, the browser sends a CONNECT request through the proxy server:

CONNECT www.example.com:443 HTTP/1.1
Enter fullscreen mode Exit fullscreen mode

The proxy then sets up a tunnel to the target server for secure communication. This method is integral to HTTPS operations but isn't typically handled directly by developers.

5. PROPFIND (WebDAV)

Use Case: The PROPFIND method is part of the Web Distributed Authoring and Versioning (WebDAV) protocol. It retrieves properties (metadata) for a resource, such as file size or last modified date, and can also list files in a directory.

Example: If you're building a file management system, PROPFIND can be used to get information about files and folders on a server:

PROPFIND /files/
Enter fullscreen mode Exit fullscreen mode

The response includes details like file properties and directory structure, useful for managing content on a remote server.

6. PURGE (Caching Servers)

Use Case: PURGE is not a standard HTTP method but is commonly used with caching servers like Varnish. It forces the server to clear cached content for a specific URL.

Example: If you've updated a webpage and want to remove its cached version, you can send a PURGE request:

PURGE /homepage
Enter fullscreen mode Exit fullscreen mode

This clears the cache for that resource, ensuring users receive the latest version on their next visit.

A Quick Recap

  • GET: Retrieve information (read-only).
  • POST: Send data to create something new.
  • PUT: Update an entire resource.
  • DELETE: Remove a resource.
  • PATCH: Partially update a resource.
  • HEAD: Retrieves only headers, not the body, to check if a resource exists or get metadata.
  • OPTIONS: Checks which HTTP methods are supported by the server for a specific URL.
  • TRACE: Echoes back the request for diagnostic purposes, allowing path tracing.
  • CONNECT: Establishes a tunnel for secure connections, typically used for HTTPS.
  • PROPFIND (WebDAV): Retrieves properties or metadata about resources, like file size or directory listing.
  • PURGE: Clears cached content for a specific resource, often used with caching systems like Varnish.

Wrapping Up

Understanding HTTP methods is crucial for building APIs and web applications that follow best practices.

Next time you're working on an API or sending a request, remember this guide and choose the right method for the job!

Happy coding!

Top comments (0)