When you visit a website through a browser, your browser is constantly sending requests and the server responds to them by providing data. That is it in a nutshell. The real process is much more complicated. What are HTTP requests and how do they work? Let's find out.
What is HTTP?
HTTP (HyperText Transfer Protocol) is a protocol for transferring hypertext. It is a set of rules for transferring data between computers and servers on the Internet. HTTP is the template for data transfer requests. It consists of text only. However, it transfers all kinds of file formats, such as video, audio, and text.
HTTP is at the top of the OSI model where applications exchange data with each other. It is part of the TCP/IP protocol family.
Another protocol is called HTTPS. The letter S in the acronym stands for secure. The principle is the same, the difference being that messages are encrypted before transmission.
What is an HTTP request?
An HTTP protocol is based on a client/server architecture where a web browser, robot, search engine, and so on act as HTTP clients, and the web server acts as a server. The session is a sequence of request-response network operations.
The client
An HTTP client sends a request to the server over a TCP/IP connection using one of the request methods, URI and protocol version, followed by a MIME-like message containing the request modifiers, client information, and any body content. Typical HTTP clients are web browsers, file downloaders, web crawlers, etc.
The server
The HTTP server receiving the request responds with a status string including message protocol version and success/error code, followed by a MIME-like message containing server information, object meta-information, and possible object body content.
How the HTTP protocol works
-
In a search box or by clicking on a link, a user enters a Uniform Resource Locator (URL). A URL begins with HTTP, indicating to the browser (the client) that the HTTP protocol must be used to obtain data.
-
Browser receives address and generates HTTP request.
-
The request is sent to your Internet Service Provider (ISP) and they send it over the Internet to a server.
-
The server reads the request. It knows how to read it because it is formatted as an HTTP request.
-
An HTTP response is generated by the server.
-
The server sends the response to the ISP and then over the Internet to your computer.
-
Your browser reads the response. It knows how to read it because it's formatted as an HTTP response.
-
Your browser displays the data.
The structure of HTTP requests
HTTP requests can look like this GET https://www.cloud4y.ru/en/ HTTP/1.1
Here you can see that the method used is GET, the resource https://www.cloud4y.ru/en/ is accessed, and the protocol is HTTP/1.1.Queries have the following structure:
-
The start line. It is used to describe the request or the status of the request. Indicates the protocol version and other information. Contains the requested resource or a response code (eg error). It is exactly one line in length.
-
HTTP header. Looks like a few lines of text specifying the request or describing the content of the message body.
-
An empty string. This is necessary as an indication that the metadata for a particular request has been successfully sent.
-
The request body. This contains information about the request or the document sent in response to the request.
The start line
Includes three main components: the request method, the request target, and the version of the transfer protocol.
Request are used in the headers of HTTP messages and allow the server and client to understand exactly what the message is about.
An HTTP method is a sequence of characters, excluding controls and delimiters, indicating the resource's basic operation. A method is usually a short, capitalized English word. The method name is case-sensitive.
Let's review a few common ones:
GET - a request for data from a server.
POST - used to send data to the server.
HEAD - identical to the GET method, except that the server response has no body. A HEAD request is usually used to get metadata, check if the resource exists (URL validation) and see if it has changed since the last request.
PUT - similar to POST except that there is no need for a server response in the case of background process execution.
The request target is actually a pointer to the URL the user needs. It consists of the protocol, the URL or IP address, and the path to the resource stored on the server. The request target may also include port and request parameters.
The version of the data transfer protocol used. Currently, common versions are HTTP/1.1 and HTTP/2. The data structure following the start line depends on the protocol.
It's worth noting that the start line can contain other useful information as well. For example, the User-Agent header contains information about the browser agent being used. Or the Referer header, the URL from which a request was made.
HTTP header
The headers look like pair _name:value_. The line is case insensitive, and the structure of the value is determined by the header. The header looks like a single, sometimes extremely long, line. Headers are used to provide additional information along with the request and may fall into one of four categories.
Headers - are essential for any type of message and are independent of the data to be transmitted.
Request Headers - contain the parameters of the requested data or define parameters with information about the client making the request.
Response Headers - provide information about the server's response.
Entity headers - allow you to describe the contents of the message body.
Request body
Not all HTTP requests may have a request body. For example for methods GET, HEAD, DELETE, and OPTIONS body usually is not needed. But for POST-type methods, it is necessary to send information to the server for an update, for example.
A request body can be divided into two categories, single-source, and multiple-source. The first category includes request bodies that consist of a single standalone file with two headers: Content-Type and Content-Length.
A multi-resource body consists of multiple parts, each containing its own bits of information. These parts are associated with HTML forms.
Advantages of HTTP
Wide range of possibilities. The protocol is designed to be extensible. HTTP is constantly evolving.
Comprehensive documentation. Easy to use for a wide range of developers, with documentation available in several languages.
The protocol is designed to work with the server and its resources, but does not include any means of navigating within them. For example, it is not possible to request a list of available files directly from the client, as you would be able to do with FTP. The client is supposed to know the URI of the required document and navigate through it using hyperlinks. This scheme is acceptable to the user but very inconvenient for the means of automatic processing and analysis of the server and its resources.