How does HTTP protocol work?

Hello everyone, welcome to my blog again. 🤓 Today, we will be learning how HTTP protocol works and how browsers send and receives data from server to client.

So, before moving to HTTP protocol, let's see what is a protocol in computer networks???

A network protocol is an established set of rules that determine how data is transmitted between different devices in the same network. Essentially, it allows connected devices to communicate with each other, regardless of any differences in their internal processes, structure or design.

Now, let’s see what HTTP is -

HTTP

It basically works on a client-server architecture model where a client is a browser(like google chrome, Mozilla, etc) which the end-user is using on his/her device while server(like AWS, GCP, personal computer, etc) is where the website is hosted. It is responsible for communication between web servers and clients. It starts with a client machine sending requests in the HTTP format. The server machine receives the request, understands it and takes appropriate action. The response again has to be formatted in a specific manner adhering to the HTTP protocol for the client to make sense of it.

So, When we enter the URL of the website we want to visit, we are asking the browser to make a request on our behalf and send it to the webserver on which that website is hosted. The server then responds with a bunch of documents(like HTML file, CSS, images, js, etc) which is then read by the browser and renders it to our screen in a presentable form instead of raw HTML code.

Each request can be seen as a transaction between client and server, and all requests are completely independent of each other. We can only specify a single resource in HTTP request line at a time meaning that we need a separate HTTP request for fetching any related files(like images, CSS, js, etc) the HTML document needs.

HTTP REQUEST METHODS —

  1. Upload some data to the server.
  2. Update some data that is already present in the server.
  3. Delete some data present in the server.

Just think how would the server know what the client wants from it?🤔

That’s why HTTP uses methods to inform the server what action to perform when the client sends a request. According to the method the client used to send request server responses with success or failure(which is denoted with different status codes. Read more about status here — https://developer.mozilla.org/en-US/docs/Web/HTTP/Status).

Let’s look at the most frequently used methods —

  1. GET request: GET requests are used to get resources from the server. Eg- fetch Html file from the server. It only retrieves the requested data from the server and doesn’t make any changes in the already present data in the server.
URL of the site
GET request on Chrome dev tools

Here, we can see as when we enter a URL in the browser, we are making a GET request to the server to return us the resources of that website(which is the HTML document in this case).

2. POST request: Post request is used to post some data into the server. Eg- Upload some data through an Html form. It basically submits the data into the server without making changes in the data which is already present.

Login Form of LinkedIn
POST request on Chrome Dev Tools

In the first screenshot, we can see that we are posting some data that will go to the server and return appropriate data according to the credentials entered by the user.

3. PUT request: Put request is used for both creating a new resource and updating the data already present in the server. Eg- changing our profile picture on social media. It makes changes in the data present in the server.

Changing the profile picture on Medium
PUT request on chrome dev tools

Here, we are changing the profile picture which will update the already present picture in the server database. This is how PUT request works.

As both POST request and PUT request can be used to create a new resource in the server. What is the difference between the two????

The key difference between PUT request and POST request is that PUT request is idempotent. Idempotency means that multiple identical requests will have the same outcome. So it does not matter if a request is sent once or multiple times. So, when we send 10 identical PUT requests, it will still be updating the existing resource only while 10 similar POST request will result in 10 new identical resources on the server. Read more about the difference between the POST request and PUT request here — https://restfulapi.net/rest-put-vs-post/.

4. DELETE request: Delete request is used to delete some data from the server that is already present there. Eg- deleting a LinkedIn post. The resource will be deleted from the server.

Deleting a LinkedIn Post
DELETE request in Chrome dev tools

The DELETE request is also idempotent, which means that multiple identical DELETE requests should have the same effect as a single request.

REFERENCES —

  1. https://www.khanacademy.org/computing/computers-and-internet/xcae6f4a7ff015e7d:the-internet/xcae6f4a7ff015e7d:web-protocols/a/hypertext-transfer-protocol-http
  2. https://www.crio.do/
  3. https://restfulapi.net/http-methods/
  4. https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP

Connect with me at LinkedIn, Instagram.

Do drop a 👏 if you liked this article.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store