DEV Community

Ruchi Vora
Ruchi Vora

Posted on • Updated on

HTTP Methods And Its Use Case

HTTP is an Application Layer Protocol and the REST API uses HTTP or HTTPS to exchange data between Client and Server using HTTP methods like :

  1. GET
  2. POST
  3. PUT
  4. PATCH
  5. DELETE

These HTTP methods are used to perform CRUD (Create ,Read ,Update ,Delete) operations on the resource.
Here resource can be a row of a relational database or a document in the case of the NoSql database or it can be any other form of a resource.

Let's understand each of the Http methods with an example of a document store. Where each document contains information about an Employee in the company.

  1. GET Method : GET method is used to retrieve the resource. For Eg : If you want to check the details of an Employee whose id is 10. Then the GET request will look like
curl --request GET 'localhost:8080/EmployeeDetail/10'
Enter fullscreen mode Exit fullscreen mode

The response of the GET request will be :

{
   'id' : '10',
   'name' : 'ruchi',
   'dob'  : '02-04-2001',
   'location' : 'new york',
   'department' : 'IT'
}
Enter fullscreen mode Exit fullscreen mode
  • The GET request can be cached. If multiple requests are made to retrieve the data of an Employee whose id is 10 , then all of them will receive the same data. So instead of hitting the server again and again for the same static data , the response of the request can be cached.
  • Also the GET request is Idempotent. Which means that even though multiple requests are made for an employee with id as 10 , it returns the same response.
  • The various HTTP response code returned by GET requests are
    • 200 (OK) - If the resource is found on the server. i.e If an Employee with id 10 exists then the HTTP response code 200 along with the resource is returned.
    • 404 ( NOT FOUND ) - If the resource does not exists on server. i.e If a document with Employee id 10 does not exists on server then HTTP response code 404 is returned
    • 400 ( BAD REQUEST ) - GET request itself is not correctly formed then the server will return the HTTP response code 400
  1. POST Method : POST method is used to create a new resource.For Eg: If you want to insert the data of a new Employee who has just joined the company , then in such case POST method is used . The POST request will look like
   curl -d '{
              'name':'ram',
              'dob' :'06-06-2000', 
              'location':'Mumbai',
              'department':'IT'
            }' -X POST -H 'Content-Type: application/json' 
   localhost:8080/EmployeeDetail
Enter fullscreen mode Exit fullscreen mode

Here , We do not pass the Id , assuming that ID is a unique
key generated by server.

  • POST request can not be cached.
  • POST request is not Idempotent . Hence, if multiple request with same data is sent to same url then two documents are created with same data but different Id , as Id being the unique key generated by the server.
  • The various HTTP response code returned by POST requests
    • 201 ( Created ) - If the new resource is created i.e If a document for new Employee is created then it returns HTTP response code 201
  1. PUT Method : PUT Method is used to perform UPSERT operation.Which means if the resource does not exists then it is created and if the resource exists then the whole resource is updated. For Eg: If we send a request with Employee Id 100 , and if there exists no Employee with Id as 100 then the new resource is created(assume that client can generate an Employee Id and server also accepts the Id generated by client). If the Employee with Id 100 exists then the whole resource(document) is updated(even though there is no change in the data).The PUT request looks like this :
     curl -d '{
               'id'  :'100'
               'name':'ram',
               'dob' :'06-06-2000', 
               'location':'Mumbai',
               'department':'IT'
            }' -X PUT -H 'Content-Type: application/json' 
   localhost:8080/EmployeeDetail
Enter fullscreen mode Exit fullscreen mode
  • PUT request can not be cached
  • PUT request is idempotent , as if N PUT requests are made then the very first request will update the resource; the other N-1 requests will just overwrite the same resource state again and again – effectively not changing anything. Hence, PUT is idempotent.
  • The various HTTP response code returned by PUT requests
    • 201 ( Created ) - If the new resource is created i.e If a document for new Employee is created then it returns HTTP response code 201
    • 200 ( OK ) - If the existing resource is updated.
    • 404 ( Not Found ) - If ID is not found or invalid.
    • 405 (Method not allowed) - If you are trying to update the Employee Id which is the unique key.
  1. PATCH Method : PATCH Method is used to make a partial update to a resource. which means if an Employee with id 10 has changed his department then in order to update only the department key PATCH Method can be used. The PATCH method looks like this
   curl -d '{'department':'IT'}' -X PATCH -H 'Content-Type: 
   application/json' localhost:8080/EmployeeDetail/10
Enter fullscreen mode Exit fullscreen mode
  • PATCH request can not be cached
  • Most of the PATCH request are Idempotent similar to PUT , but some PATCH requests are not Idempotent.
  • The various HTTP response code returned by POST requests
    • 200 ( OK ) - If the existing resource is partially updated.
    • 404 (Not Found) - If ID is not found or invalid
    • 405 (Method not allowed) - If you are trying to update the Employee Id which is the unique key.
  1. DELETE Method : DELETE Method is used to delete a resource.For Eg: If we want to delete Employee with Id as 10 then the DELETE method looks like this
   curl -X DELETE -H localhost:8080/EmployeeDetail/10
Enter fullscreen mode Exit fullscreen mode
  • DELETE request can not be cached.
  • DELETE operations are idempotent. If you DELETE a resource, it’s removed from the collection of resources. Repeatedly calling DELETE API on that resource will not change the outcome – however, calling DELETE on a resource a second time will return a 404 (NOT FOUND) since it was already removed.

Latest comments (0)