๐ 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
- Idempotent Methods - Methods which doesn't change state of the server even when the same (identical) request is made multiple times in a row.
- Safe Methods - Methods which doesn't change any resource in a server.
- Cacheable Methods - 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
-
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
-
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
- has response body - YES
-
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
-
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
-
DELETE - The HTTP DELETE request method deletes the specified resource.
- has request body - May
- Idempotent - YES
- Safe - NO
- Cacheable - NO
- has response body - May
-
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
-
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
-
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
-
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
๐โ๐จ 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!!!โค๏ธ
Top comments (0)