What Is HTTP? Different HTTP Methods And Status Codes Explained With Examples


Share On        


Hello there! This article is very interesting but on the other hand more of non practical one. But tons of things to learn so stay tuned till last.


In this tutorial you will learn

  1. Introduction To HTTP
  2. Different HTTP Request Methods
  3. Explanation Of HTTP Request Methods With Examples
  4. GET v/s POST Requests
  5. PUT v/s PATCH Requests
  6. HTTP Status Codes


For REST API Documentation please use this link Postman REST Demo Documentation Link. This is not the complete working code for this. In this demo there is no security for the API's. For securing API's I will cover in another article.
Once you open this Postman Documentation Link you can change to other languages too :)

1) Introduction To HTTP


HTTP is an acronym of Hyper Text Transfer Protocol. This protocol is responsible for communication between the Server's & Client's.


Server's are the devices or computers that provides the requested data. Usually they host the website/software within them for Eg: facebook.com, google.com.


Client's are the devices or software within device, which is used to fetch the data from Server. Eg: Google Chrome, FireFox, Safari


The communication between the Server & Client is exchanged via HTTP Requests & HTTP Responses. When user from Client (Eg: browser - Google Chrome, Safari, Firefox) request for some data to Server (Eg: facebook.com/google.com), the Server returns with Data & Headers.

Every time while Client requesting data to server it tell what kind of data it needs along with headers. Based on the needs of the data from Client, Server responds with that data with headers


Each & every request are independent. Which usually we call as Stateless. Now days web applications uses sessions & cookies to remember users activity.


2) Different HTTP Request Methods


The famous HTTP methods used to exchange the data between the Client & the Server are as follows


  1. GET
  2. POST
  3. PUT
  4. PATCH
  5. DELETE
  6. HEAD
  7. UNLINK
  8. OPTIONS
  9. LINK

3) Explanation Of HTTP Request Methods With Examples


NOTE: For all the requests I will give Command Line CURL. If you click this Postman Documentation Link you can change to other languages too :)


I. GET REQUEST


When you want to only fetch the data from server use GET request method. The server will send the requested data along with status codes.


GET request is less secure

  1. Because the data you will pass in the form filled will be shown in the URL.
  2. Data shared in URL has limitation on the size. If you have very large to be added then this one is definitely a bad choice.


Example:


Get list of all users


CURL Example (Get Users List)


curl --location --request GET 'https://reqres.in/api/users?page=1&per_page=2'

II. POST REQUEST


Use POST request method when you want to send the data to server, & server upon receiving this data saves the data to respective database.

Once the data is saved it returns data, headers & status codes.


POST request is more secure that GET request


  1. POST request data is posted securely rather than exposing it in URL
  2. POST data can very as much larger you need to need to break your head on restricting it.


Example:


New users registrations. Login Form & any other form data filling page.


CURL Example (Create New User)


curl --location --request POST 'https://reqres.in/api/users' \
--data-urlencode 'name=morpheus' \
--data-urlencode 'job=leader'

III. PUT REQUEST


PUT request is similar to POST request and you can send data to server. But the only difference is PUT is used to update the data which was saved earlier by POST request.


When you want to replace the database table record and add new record while updating you will use PUT request method.


Example:


User will have hobbies, when we update user hobbies instead of checking if the new updated hobby lists is added or not, if new hobby list exists or not, then adding it. This will be pretty much complex if we hit database for the same.

Instead we remove all the user hobbies first and then insert the new hobbies.


CURL REQUEST (Update User Details)


curl --location --request PUT 'https://reqres.in/api/users/266' \
--data-urlencode 'name=Channaveer' \
--data-urlencode 'job=Hakari'

IV. PATCH REQUEST


PATCH is similar to that of POST request and you can send the data to server for updating the data.


When you want to patch the existing database record you use PATCH request method.


Example:


If you want to update the user details like password then you will only update that password, you don't want to replace the whole user record as user might be having other dependant details.


CURL REQUEST (Update User)


curl --location --request PATCH 'https://reqres.in/api/users/266' \
--data-urlencode 'name=Channaveer' \
--data-urlencode 'job=Hakari'

V. DELETE REQUEST


When you need to delete the resource from the database you will use DELETE request method.


Example:


If you want to delete hobbies of user then you can delete it with this request method.


CURL REQUEST (Delete User)


curl --location --request DELETE 'https://reqres.in/api/users/266'

VI. HEAD REQUEST


HEAD request is same as that of GET request method. But the only difference between GET & HEAD is HEAD - Only return headers, where as GET return data with headers.


So when you don't need the data from the server then you use this particular request method.


Example:


If you want check pings to your server then you don't want any return data. We call it as HeartBeat check


CURL REQUEST (Check If Server Is Live)


curl --location --request HEAD 'https://someurl.in/api/heartbeat'



VII. ULINK REQUEST


You use UNLINK request to remove any uploaded or particular path files. I basically use this for that.


Example:


If you wan to delete the obsolete invoice or database backup then you use this request method.


GET v/s POST Requests


As I had explained you earlier in this article

  1. GET is less secure than POST request as GET sends all the form data in URL and someone can sneak into it very easily
  2. GET request has size limited of the data sent in the URL than that of POST. In POST you can sent very huge data to server.


5) PUT v/s PATCH Requests


As explained above

  1. Use PUT when you want to update records by replacing the whole data. Example - If you want to remove user hobbies then this is the best one.
  2. PATCH - If you want to just update the same record without deleting. Example - User password.

6) HTTP Status Code


When you perform HTTP Requests & HTTP Responses you will come across weird numbers like 200, 201, 301, 302, 404, 400. What the heck is this? Sometimes you might even see weird 404 while browsing the web pages.


These are nothing but the HTTP status codes. To put it simple HTTP status codes are returned by the server when client request for data.


HTTP status codes are divided into 5 categories


1xx informational response – the request was received, continuing process

2xx successful – the request was successfully received, understood, and accepted

3xx redirection – further action needs to be taken in order to complete the request

4xx client error – the request contains bad syntax or cannot be fulfilled

5xx server error – the server failed to fulfil an apparently valid request


Source: Wikipedia


The most common HTTP status codes are as follows


200 (OK) - The server responds with 200 when the request data is found with the server. Eg: Get product details, get user details etc.


201 (Created) - When a new record is create on server side then it will return with 201 status code. Eg: New user registered, New bug created.


301 (Moved Permanently) - This HTTP is received when something your looking for is moved to other site or redirected permanently. Eg if big company acquires small companies then instead of closing acquired company website they will point it to theirs with this header.

When you try https://www/stackcoder.in the it will redirect you to https://stackcoder.in


302 (Moved Temporarily) - This one you will get when you try to redirect from one path to another path in the same website. Eg: when you go for https://stackcoder.in then you will be redirected to https://stackcoder.in/posts.


400 (Bad Request) - If you don't pass the proper data to server then you will get validations errors, or else when you don't pass something which is required in server then you get 400 status.


401 (Unauthorised) - When you try to request the resource which you don't have permission. Eg: When you try to delete admin user being as normal client then you cant do it, there you will get this status error.


404 (Not Found) - If the page your looking out for is not found then you will get 404 status.


405 (Method Not Allowed) - When you try to POST the data for GET request or try to directly add POST URL in the web address bar then you will get this kind of error.


Conclusion


HTTP is like a sea. I have tried my best to cover very basics for you guys. I know I might have skipped a lot of things, but I will assure you guys that I will bounce back with continued follow articles for this.




AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing