This material is part of the “Introduction to Enterprise Development” series. Previous articles:
Hello! Today we will understand the HTTP and HTTPS protocols. But first, let's clarify one point: we are talking about data transfer protocols over the network at the application layer of the OSI model. As you remember, we discussed the OSI model in one of the previous articles. And if you don’t remember, here it is .
It’s worth noting right away that the HTTP protocol consists of text only. Well, we are most interested in the structure in which this text is located. Each message consists of three parts:
What is a data transfer protocol
This is the name given to the generally accepted agreement, thanks to which developers of different services send information in a single form. For example, using Google Chrome, you can get information from both Facebook and Twitter, because the developers transmit it using the standard HTTP protocol, and your browser can handle it. The uniform rules are also very convenient for the server-side developers themselves: there are many libraries that can convert information for you and send it using the required protocol. HTTP was originally conceived as a protocol for transferring HTML pages. This was the case for a long time, but now programmers often transfer both strings and media files over it. Overall, this protocol is versatile and flexible, and it's really easy to use. Now let’s figure out how to do this.
HTTP structure
It’s worth noting right away that the HTTP protocol consists of text only. Well, we are most interested in the structure in which this text is located. Each message consists of three parts:
- Starting line—defines service data.
- Headers - description of message parameters.
- Message body (Body) - message data. Must be separated from headings by a blank line.
What a simple HTTP request looks like
GET / HTTP/1.1
Host: javarush.com
User-Agent: firefox/5.0 (Linux; Debian 5.0.8; en-US; rv:1.8.1.7)
The starting line contains:
- GET - request method;
- / — request path (path);
- HTTP/1.1 - version of the data transfer protocol.
- Host — the host to which the request is addressed;
- User-Agent is the client that sends the request.
POST /user/create/json HTTP/1.1
Accept: application/json
Content-Type: application/json
Content-Length: 28
Host: javarush.com
{
"Id": 12345,
"User": "John"
}
The request is sent to javarush.com/user/create/json, protocol version is HTTP/1.1. Accept specifies what response format the client expects to receive, Content-Type specifies what format the message body is sent in. Content-Length - the number of characters in the body. An HTTP request can contain many different headers. More details can be found in the protocol specification .
HTTP responses
After receiving the request, the server processes it and sends a response to the client:HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Length: 98
<html>
<head>
<title>An Example Page</title>
</head>
<body>
<p>Hello World</p>
</body>
</html>
The starting line in the response contains the protocol version (HTTP/1.1), Status Code (200), Status Description (OK). Headings indicate the type and length of content. The body of the response contains the HTML code that the browser will draw into the HTML page.
Response Status Codes
Everything is clear with the message body and headers, but it’s worth saying a few words about status codes. Response Status Codes are always three digits, and the first digit of the code indicates the category of the response:- 1xx - informational. The request has been received, the server is ready to continue;
- 2xx - successful. The request has been received, understood and processed;
- 3xx - redirection. The following steps must be performed to process the request;
- 4xx - client error. The request contains errors or does not comply with the protocol;
- 5xx - server error. The server was unable to process the request, although it was composed correctly;
- 200 OK — the request was received and successfully processed;
- 201 Created — the request was received and successfully processed, resulting in the creation of a new resource or its instance;
- 301 Moved Permanently - the requested resource has been moved permanently, and subsequent requests to it must occur at the new address;
- 307 Temporary Redirect - the resource has been moved temporarily. For now, you can access it using automatic redirection;
- 403 Forbidden - the request is clear, but authorization is required;
- 404 Not Found - the server did not find the resource at this address;
- 501 Not Implemented - the server does not support functionality to respond to this request;
- 505 HTTP Version Not Supported - the server does not support the specified version of the HTTP protocol.
GO TO FULL VERSION