Http Request and Response

Raza
8 min readNov 24, 2023

An HTTP (Hypertext Transfer Protocol) request is a fundamental part of web communications, forming the basis of data exchange on the internet. It’s a protocol used for fetching resources, such as HTML documents, images, and other files, from web servers. The communication between a client (like a web browser) and a server occurs through these HTTP requests. Here’s a breakdown of how it works:

  1. Request Line: An HTTP request starts with a request line, which includes the following:
  • Method: This indicates the action to be performed on the resource. Common methods include GET (retrieve a resource), POST (submit data to the server), PUT (update a resource), DELETE (remove a resource), and others.
  • Request URL: The URL of the resource being requested.
  • HTTP Version: The version of the HTTP protocol being used.
  1. Request Headers: Following the request line are headers, which provide additional information about the request. This can include details about the client’s browser, the types of responses that the client will accept, cookies, and more.
  2. Blank Line: A blank line indicates the end of the headers section.
  3. Request Body (Optional): Not all requests have a body. Bodies are typically included in POST or PUT requests, where you’re sending data to the server (like form inputs or file uploads). The body contains the data being sent.

The server, upon receiving this request, processes it and sends back a response. The response typically contains a status line (indicating if the request was successful or if there were errors), response headers (providing metadata about the response), and often a response body (which contains the requested resource or a message).

In summary, an HTTP request is a structured way for a client to ask for resources or actions from a server, and it’s an essential component of how the internet functions, allowing for the exchange of information and resources across the web.

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

  1. 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.
  2. Browser receives address and generates HTTP request.
  3. The request is sent to your Internet Service Provider (ISP) and they send it over the Internet to a server.
  4. The server reads the request. It knows how to read it because it is formatted as an HTTP request.
  5. An HTTP response is generated by the server.
  6. The server sends the response to the ISP and then over the Internet to your computer.
  7. Your browser reads the response. It knows how to read it because it’s formatted as an HTTP response.
  8. Your browser displays the data.

The structure of HTTP requests

HTTP requests can look like this GET https://www.cloud4u.com/ HTTP/1.1

Here you can see that the method used is GET, the resource https://www.cloud4u.com 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.

HTTP response

An HTTP response is what a server sends back to the client after receiving and processing an HTTP request. It is a key part of the data exchange in web communications, essentially being the server’s answer to the client’s request. Here’s a breakdown of the components of an HTTP response:

  1. Status Line: This is the first line of the response and includes:
  • HTTP Version: Indicates the HTTP protocol version used (e.g., HTTP/1.1).
  • Status Code: A three-digit number that indicates the outcome of the request. Common status codes include 200 (OK, request succeeded), 404 (Not Found, the resource can’t be found), 500 (Internal Server Error), etc.
  • Status Text: A brief, human-readable explanation of the status code (e.g., OK, Not Found, Internal Server Error).
  1. Response Headers: These are key-value pairs providing additional information about the response. They can include details such as the server type, content type, content length, caching policies, set cookies, and other metadata.
  2. Blank Line: A blank line signifies the end of the header section.
  3. Response Body: This part of the response contains the actual data or resource that the client requested. For example, it could be an HTML file, JSON data, an image, etc. In some cases, particularly when the response indicates an error (like a 404), the body might contain a message explaining the error.

The combination of the status code and response body allows the client to understand whether the request was successful and to process the returned data accordingly. For instance, a web browser would render the HTML from the response body, a REST API client might process JSON data, or handle redirection if the status code indicates a redirection (like 301 or 302).

In summary, the HTTP response is a crucial component in the client-server communication over the web, allowing servers to provide the necessary data, resources, or feedback in response to the client’s requests.

--

--

Raza

Expert in Linux Systems, Kubernetes, Cloud Technologies, Virtualization Technologies, Python Programming and Storage Systems.