9 HTTP request methods explained!
Tags: webdev, javascript, http, programming
Published: 2024-05-19 05:47:48

HTTP methods are the backbone of Integration and getting request/response from the server. These methods create the fundamentals of programming and handling requests to/from the client and server side.

In this blog post, I will discuss about some of the most Important HTTP Methods that every developer must know about.

Let's get started.

HTTP (Hypertext Transfer Protocol) specifies a collection of request methods to specify what action is to be performed on a particular resource. The most commonly used HTTP request methods are GET, POST, PUT, PATCH, and DELETE. These are equivalent to the CRUD operations (create, read, update, and delete). But there are total of 9 HTTP Methods that you must be aware of.

HTTP methods can be judged on the basis of three properties:

1. Safe – if it doesn't affect the server state

2. Idempotent – no matter how many times you make an API call, the result remains the same

3. Cacheable – can a response be cached?

Here are some characteristics of all HTTP Methods: 

## 1. GET

Widely used. 

GET method is used to request the desired resource from the server. The GET method is used to request any of the following resources:

- A webpage or HTML file.
- An image or video.
- A JSON document.
- A CSS file or JavaScript file.
- An XML file.

It doesn't affect the server state.

✅ Safe
✅ Idempotent
✅ Cacheable

## 2. POST

POST method is completely opposite to GET.

It is used to submit the data to the server and hence it changes the server state.

The data sent to the server is typically in the following form:

- Input fields from online forms.
- XML or JSON data.
- Text data from query parameters.

❌ Safe
❌ Idempotent

## 3. PUT

PUT method is used to update the resource on the server which is already present.

It replaces the entire content at a particular location with data that is passed in the body payload.

The HTTP PUT request method includes two rules:

- A PUT operation always includes a payload that describes a completely new resource definition to be saved by the server.
- The PUT operation uses the exact URL of the target resource.

If a resource exists at the URL provided by a PUT operation, the resource’s representation is completely replaced. If a resource does not exist at that URL, a new resource is created.

❌ Safe
✅ Idempotent
❌ Cacheable

## 4. PATCH

PATCH is similar to PUT request, but the only difference is, it modifies a part of the data. It will only replace the content that you want to update.

❌ Safe
❌ Idempotent
❌ Cacheable

## 5. DELETE

As the name suggests, it is used to delete the data on the server.

❌ Safe
✅ Idempotent
❌ Cacheable

## 6. HEAD

The HEAD method in HTTP is similar to GET, but it requests only the headers of a resource without fetching the actual body content.

The HTTP HEAD method is commonly used to check the following conditions:

- The size of a resource on the server.
- If a resource exists on the server or not.
- The last-modified date of a resource.
- Validity of a cached resource on the server.

The following example shows sample data returned from a HEAD request:

```
HTTP/1.1 200 OK 
Date: Fri, 19 Aug 2023 12:00:00 GMT 
Content-Type: text/html 
Content-Length: 1234 
Last-Modified: Thu, 18 Aug 2023 15:30:00 GMT
```

✅ Safe
✅ Idempotent
✅ Cacheable

## 7. OPTIONS

OPTIONS in HTTP retrieves available communication options for a resource. It tells supported methods and server restrictions. 

The following is a sample response to an HTTP OPTIONS method call to a server:

```
OPTIONS /example/resource HTTP/1.1 
Host: www.example.com HTTP/1.1 200 OK 
Allow: GET, POST, DELETE, HEAD, OPTIONS 
Access-Control-Allow-Origin: * 
Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS 
Access-Control-Allow-Headers: Authorization, Content-Type 
```

Used for preflight requests.

✅ Safe
✅ Idempotent
❌ Cacheable

## 8. CONNECT

It establishes a TCP connection through a proxy server to the actual server. It's commonly used in HTTPS connections to set up secure communication between a client and a server through an intermediary proxy server.

❌ Safe
❌ Idempotent
❌ Cacheable

## 9. TRACE

TRACE HTTP method is useful in debugging.

When a TRACE request is sent, the server reflects the received request to the client, letting it see any changes made as it passes through proxies or gateways.

✅ Safe
✅ Idempotent
❌ Cacheable

---

That's a wrap. I hope that you've enjoyed reading this article.

Please share this information with others and show some love. See ya...

Follow me on socials: 

Twitter: https://x.com/Hy_piyush
LinkedIn : https://www.linkedin.com/in/piyush-kesarwani-809a30219/