The HTTP
Ever wondered how your browser fetches a webpage or how data flows between servers? Understanding HTTP is key to decoding the web’s communication!
What is HTTP?
HTTP stands for Hypertext Transfer Protocol. It is used to exchange information between a client and a web server. This is a client-server protocol, meaning that the client initiates a request, and the server responds to that request accordingly.
HTTP is an application layer protocol that uses TCP or TLS to send messages over a secured connection.
What is a protocol?
A protocol is a set of rules, a contract between two devices. It’s like two devices agreeing on what language to communicate in. There are various types of protocols, and they dictate how to interpret and format data so that other devices can understand it.
HTTP Session flow?
There are three phases in the HTTP session.
Before HTTP/1.1, the connections were closed after a single request and response cycle, the client has to go through all the above three phases to make a new request. In and After HTTP/1.1, the underlying transport layer connection stays open so we can perform the 2 and 3 phases any number of times.
HTTP Message?
Before HTTP/2, messages in the HTTP protocol were text-based, making them easy to read and understand. However, in HTTP/2, the messages are encapsulated in binary framing, which makes them more challenging to interpret. Despite this change, the underlying structure of the messages remains the same. So, let’s use HTTP/1.1 for better readability.
Structure of HTTP?
The structure of the request and response of the HTTP message is the same.
POST / HTTP/1.1\r\n
Host: developer.mozilla.org\r\n
User-Agent: curl/8.6.0\r\n
Accept: */*\r\n
Content-Type: application/json\r\n
Content-Length: 345\r\n
\r\n
{
"data": "ABC123"
}
That is how a request looks, separated by carriage-return and line-feed characters. Now we know which part is the start line, headers, and body while parsing.
HTTP Request?
A Post request after the user submits the form on the web page.
POST /users HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 50
name=FirstName%20LastName&email=bsmth%40example.com
Request Start-line?
<method> <request-target> <protocol>
Request Headers?
Headers are the metadata about the message.
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 50
These headers are used as an example for the above request. Headers are case-insensitive and are separated by a colon (:). Each header has a distinct meaning. For example, a content-type header describes the format of the data. content-length header describes the length of the body and many more.
Request body?
This is an HTTP body used as an example for the above request.
name=FirstName%20LastName&email=bsmth%40example.com
Only PUT, POST and PATCH methods have the request body. A request body is what carries the information to the server. The information we are looking at is in URL-encoded form and its length is 50. This information is provided in headers. so that the server knows how to decode the body and the expected length of the body.
HTTP Response?
The response from the servers is pretty much the same but the start line indicates:
“Keep exploring, keep coding, and stay curious!”