The Hypertext Transfer Protocol
Web·9 min read·Jan 1, 2025
The Hypertext Transfer Protocol (HTTP) is a protocol for transmitting hypermedia documents, such as HTML. While originally designed for communication between web browsers and web servers, it is nowadays also used for other purposes, such as server to server communication.
It follows a classical client-server model, with a client opening a connection to make a request, then waiting until it receives a response.
A typical HTTP session consist of 4 phases where:
- The client initiates a connection with the server using a URL and a port number.
- The client sends its request and waits for the response.
- The server processes the request.
- The server sends back its response.
It's important to note that HTTP is a stateless protocol, meaning that the server does not keep any data (state) between two requests.
The client HTTP request
An HTTP request is a message sent by a client to a named host in order to access a resource or perform an action on a server, which contains:
- A request line.
- A set of HTTP headers.
- An optional message body.
The request line
The request line contains:
- An HTTP method — also called a verb — which is a one-word command that specifies the desired action to be performed by the server.
- A relative URL which is the path component of the URL for the request.
- The HTTP version number.
Example
For example, this request line indicates that the client wants to retrieve the /login page using the version 1.1 of the HTTP protocol:
GET /login HTTP/1.1
Where:
GETis the HTTP method./loginis the relative URL.HTTP/1.1is the HTTP version number.
The request headers
The HTTP headers are composed of a name and a value, and provide information about the message, the client, and the way in which the client wants to communicate with the server.
Example
For example:
"Accept: text/html"means that the client expects the server to respond with an HTML page."Content-Type: application/json"means that the message body of the request has the JSON format.
The request message body
The message body is the actual content of the request, which can eventually be encoded or broken into data chunks.
Note that this message body is optional, as indeed not all requests need to send data to the server, like HTTP GET requests for instance, which ask the server to send a resource and not the other way around.
Example
For example, this request indicates that the client wants to send an URL-encoded string to the server:
POST /auth/signin HTTP/1.1
Host: api.learnbackend.com
Accept: application/json
Accept-Language: en-us
Content-Length: 40
Content-Type: application/x-www-form-urlencoded
email=jdoe@mail.com&password=helloworld
Where:
- The
POST /auth/signin HTTP/1.1request line indicates an HTTP POST request to the/auth/signinendpoint. - The
Host: api.learnbackend.comheader indicates that the request is sent to the server located atapi.learnbackend.com. - The
Accept: application/jsonheader indicates that the client accepts responses in the JSON format. - The
Accept-Language: en-usheader indicates that the client accepts responses in the American English language. - The
Content-Length: 40header indicates that the message body is 40 characters long. - The
Content-Type: application/x-www-form-urlencodedheader indicates that the message body is in the URL-encoded format. - The
email=jdoe@mail.com&password=helloworldstring represents the message body of the request.
The server HTTP response
An HTTP response is a message sent by a server to a client in response to an HTTP request which provides the client with the requested resource, or informs it that the requested action has been carried out; or else, that an error occurred while processing it.
An HTTP response contains:
- A status line.
- A set of HTTP headers.
- A message body.
The response status line
The status line — similar to the request line — contains:
- The HTTP version number.
- A status code, which is a three-digit number indicating whether a request has been successfully completed or if something went wrong.
- A reason phrase, which is human-readable text that summarizes the meaning of the status code.
Example
For example, the following status line indicates that the request was successfully processed by the server:
HTTP/1.1 200 OK
And the following status line indicates that the requested resource doesn't exist on the server:
HTTP/1.1 404 Not Found
The response headers
The HTTP headers are composed of a name and a value, and provide information about the response and the server that sent it.
They are meant to assist the client in processing or displaying the response to a user, as well as caching it for future use.
In the case of an unsuccessful request, headers can also be used to tell the client what it must do to complete its request successfully.
Example
For example:
"Content-Type: application/json"means that the message body of the response is in the JSON format."X-Powered-By: Express"means that the server is using the Express Framework.
The response message body
The message body is the actual content of the response, which may contain either the requested resource, some information about the status of the requested action, or in the case of an error, some additional information about the reasons for that error.
Example
For example, this response indicates that the server was able to process the client's request successfully:
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 37
X-Powered-By: Express
{"firstName":"John","lastName":"Doe"}
Where:
- The
HTTP/1.1 200 OKstatus line indicates that the request was processed successfully. - The
Content-Type: application/jsonheader indicates that the message body is in the JSON format. - The
Content-Length: 37header indicates that the message body is 37 characters long. - The
X-Powered-By: Expressheader indicates that the server uses the Express framework. - The
{"firstName":"John","lastName":"Doe"}string represents the message body of the response.
🗒️ Summary
Here's a summary of what you've learned in this lesson:
- HTTP is a protocol used for transmitting hypermedia documents between clients and servers, or arbitrary data between servers and servers.
- An HTTP request is composed of a request line, a set of HTTP headers, and optionally a message body.
- An HTTP response is composed of a status line, a set of HTTP headers, and optionally a message body.