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:
- There are several different protocols beside HTTP and its encrypted version (HTTPS), such as the File Transfer Protocol (FTP) and its secure counterpart, the SSH File Transfer Protocol (SFTP).
- The host could either be an actual IP or a hostname. When a hostname is selected, there is another player, a Domain Name Server (DNS), that acts as a phonebook between hostname and physical addresses. The DNS translates hostnames to IPs.
- The path is the resource desired in the server and it is always absolute.
- The query string is something added to a path after a question mark. It is a series of key value pairs in the form key=value and they are separated by an & sign.
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:
- GET: A representation of the selected resource
- HEAD: Similar to GET, but without any response body
- POST: Submits a resource to the server, usually a new one
- PUT: Submits a new version of a resource
- DELETE: Removes a resource
- PATCH: Requests specific change to a resource
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:
- The first line is a space separated triplet: method—path—protocol.
- It's followed by one line per header.
- One empty line as separator.
- The optional request body.
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:
- 100: Information/request was received and will have further processing
- 200: A successful request; for instance, OK 200 or Created 201
- 300: A redirection to another URL, temporary or permanent
- 400: A client-side error, such as Not Found 404 or Conflict 409
- 500: A server-side error, such as Internal Server Error 503
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>