DEV Community

Cover image for Hello, I'm HTTP and these are my request methods👋🏻
Kaustubh Joshi
Kaustubh Joshi

Posted on

Hello, I'm HTTP and these are my request methods👋🏻

😄 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 - 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 - Methods which doesn't change any resource in a server.
  3. 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

  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.
  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
image

👁‍🗨 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

📚 References

Top comments (0)