HTTP protocol

The HTTP protocol is the cornerstone of a web server. Its design started in 1989. The main usage of HTTP is the request and response paradigm, where the client sends a request and the server returns back a response to the client. 

Uniform Resource Locators (URLs) are unique identifiers for an HTTP request, and they have the following structure:

Part Example
Protocol http
:// ://
Host www.website.com
Path /path/to/some-resource
? ?
Query (optional) query=string&with=values

 

From the preceding table, we can derive the following:

HTTP is a textual protocol and it contains some of the elements of the URL and some other information—method, headers, and body.

The request body is the information sent to server, such as form values, or uploaded files. 

Headers are metadata relative to the request, one per line, in a Key: Value; extra data form. There is a list of defined headers with specific functions, such as Authorization, User-Agent, and Content-Type.

Some methods express the action to execute on a resource. These are the most commonly used methods:

This is what an HTTP request would look like:

POST /resource/ HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.website.com
Content-Length: 1024
Accept-Language: en-us

the actual request content
that is optional

From the preceding code, we can see the following:

For each request, there is a response that has a structure that is pretty similar to an HTTP request. The only part that differs is the first line that contains a different space-separated triplet: HTTP version—status code—reason.

The status code is an integer that represents the outcome of the request. There are four main status categories:

This is a what an HTTP response would look like:

HTTP/1.1 200 OK
Content-Length: 88
Content-Type: text/html

<html>
<body>
<h1>Sample Page</h1>
</body>
</html>