Hello, I'm HTTP and these are my request methods👋🏻
Tags: programming, beginners, webdev, codenewbie
Published: 2021-10-08 12:44:51

# 😄 Introduction
HTTP (Hyper Text Transfer Protocol) is heart of all the websites and that's one of the many reason to know the different **request methods** this protocol provides, be it for REST API development, a frontend dev consuming APIs or even for cracking an Interview.

This post will provide you all the information you need to understand HTTP a better and a simpler way.


## 💻 Semantics
1. [Idempotent Methods](https://developer.mozilla.org/en-US/docs/Glossary/Idempotent) - Methods which doesn't change **state of the server** even when the **same (identical)** request is made *multiple* times in a row.
2. [Safe Methods](https://developer.mozilla.org/en-US/docs/Glossary/Safe/HTTP) - Methods which doesn't change any **resource** in a server.
3. [Cacheable Methods](https://developer.mozilla.org/en-US/docs/Glossary/cacheable) - Methods whose response can be **cached** on the **client side** saving the REST call to the server.


> **Note:** All SAFE methods are IDEMPOTENT but not vice versa.


## 📬 HTTP Request Methods

1. **_GET_** - Represents a request for specific source. Only use to fetch data.
    * has request body - NO
    * Idempotent - YES
    * Safe - YES
    * Cacheable - YES
    * has response body - YES
2. **_POST_** - Used to send data to the server. Ideally should use to **create** new resource on the server.
    * has request body - YES
    * Idempotent - NO (placing an order several time)
    * Safe - NO
    * Cacheable - [Only if freshness information is included](https://stackoverflow.com/questions/626057/is-it-possible-to-cache-post-methods-in-http)
    * has response body - YES
3. **_PUT_** - Used to **create** new resource or **update** an existing resource with the request body containing the newly-updated representation of the original resource.
    * has request body - YES
    * Idempotent - YES
    * Safe - NO
    * Cacheable - NO
    * has response body - NO
4. **_PATCH_** - Used to **partially** update data with the provided **identifier**, if **absent** it will throw an **exception**.
    * has request body - YES
    * Idempotent - NO
    * Safe - NO
    * Cacheable - NO
    * has response body - YES
5. **_DELETE_** - The HTTP DELETE request method **deletes** the specified resource.
    * has request body - May
    * Idempotent - YES
    * Safe - NO
    * Cacheable - NO
    * has response body - May
6. **_OPTIONS_** - The OPTIONS method describes the **communication options** for the target resource. 
    * has request body - NO
    * Idempotent - YES
    * Safe - YES
    * Cacheable - NO
    * has response body - YES
7. **_HEAD_** - Returns headers of the HTTP request **without** response body. Same as GET without response body.
    * has request body - NO
    * Idempotent - YES
    * Safe - YES
    * Cacheable - YES
    * has response body - NO
8. **_CONNECT_** - The HTTP CONNECT method starts two-way communications with the requested resource. It can be used to open a **tunnel**.
    * has request body - NO
    * Idempotent - NO
    * Safe - NO
    * Cacheable - NO
    * has response body - NO
9. **_TRACE_** - The HTTP TRACE method performs a **message loop-back test** along the path to the target resource, providing a useful debugging mechanism.
    * has request body - NO
    * Idempotent - YES
    * Safe - YES
    * Cacheable - NO
    * has response body - NO


> **Note:** PUT will either update or create new resource where PATCH will always update it, if the provided identifier is absent on the server, it will throw an exception. (ex. invalid product id)


## 🏮 Browser Compatibility
This image is taken from [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/molk7gdrkjdtjbuionvk.png)
 

## 👁‍🗨 Conclusion
This article should provide you a birds-eye-view of all the HTTP request methods in less than 10 mins.
I hope this post will help you in future, while choosing the correct method for your API or a last min study for your interview.

HAPPY CODING!!!❤️

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8kta7fs4e4x6v9ez16j.gif)

## 📚 References
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
* https://stackoverflow.com/questions/31089221/what-is-the-difference-between-put-post-and-patch