The HTTP Protocol: Foundation of Web Communication

I am a technical writer and software engineer
HTTP is the standard communication protocol used to transfer messages via the Internet. It uses a message-based model allowing a client to send a request to the server. The server then returns a response message.
What are HTTP Requests and HTTP Responses?
HTTP Request
The client sends an HTTP request to the server. It consists of a headers, a blank line, and a message body.
Here’s a HTTP request example:
GET /home HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Cookie: sessionId=abc123; username=allan
Referer: https://www.example.com/previous-page
The first line of the request consists of three items: the HTTP method, the requested URL, and the HTTP version.
The HTTP method indicates what the request will do. For example, a GET method retrieves a resource from a web server while a POST method adds data to a web server.
The URL is the name of the resource being requested. It can include a query string containing parameters the client sends to the resource.
There are two HTTP versions: 1.0 and 1.1. Most browsers use 1.1.
An HTTP request also includes a Referer header that indicates the URL from which the request originated. Others include User-Agent, Host, and Cookie headers.
HTTP Response
This is the response message from the web server.
Here’s an HTTP response example:
HTTP/1.1 302 Found
Date: Wed, 30 Oct 2024 15:30:00 GMT
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: Wed, 30 Oct 2024 15:30:00 GMT
Location: https://www.example.com/redirect-target
Server: nginx/1.21.3
WWW-Authenticate: Basic realm="Access to the site", charset="UTF-8"
X-Frame-Options: DENY
The first line of the response consists of the HTTP version, a status code such as 200, and a reason phrase that expounds on the response status.
Other notable headers in an HTTP response include Pragma, Content-Length, Content-TYpe, and Server.
HTTP Methods
GET method retrieves resources from a web server. It can also be used to send parameters to the resource.
POST method is used to perform actions. Unlike the GET method, you can send a request parameter in the message body and URL query string.
PUT method loads a specified resource to the server, using content in the body of the request.
TRACE Method is used for diagnostic purposes. When used, the server returns in the response body the exact contents of the request message it received.
HTTP Headers
There are many HTTP header types, which can be used for HTTP requests and responses. The following are general headers:
Connection that tells the other end of communication whether it should close the TCP connection or keep it open to allow continuous communication.
Content-Encoding indicates which type of encoding is being used. For example, gzip for compressing responses.
Content-Length specifies the length of a message in bytes.
Content-Type indicates the type of content in the message body such as img/jpeg
Transfer-Encoding specifies the encoding that was performed on the message body.
HTTP Request Headers
HTTP request headers provide the server with more information about the request. They can include the following:
Accept tells the server which content the client will accept, such as office document formats.
Accept-Encoding tells the server the type of encoding the client will accept
Authorization submits credentials to the server for one of the HTTP authentication types.
Cookie submits cookies to the server that the server previously issued.
Host indicates the hostname that was in the URL being requested.
Origin indicates the domain where the request originated
Referer shows the URL from which the current request has originated.
User-Agent shows the browser or software from where the request originated.
HTTP Response Headers
Once the server receives the request from the client, it sends back response headers. They contain instructions on how the client should handle the response.
Cache-control indicates the caching directives like no-cache.
Expires indicates how long the message body contents are valid.
Location is used when a page has a redirection response. It specifies the target of the redirect.
Pragma indicates caching directives to be passed to the browser.
Server shows information about the web server software in use.
WWW-Authenticate is used in responses with 401 status code. It shows the type of authentication the server supports.
X-Frame-Options indicates how and where the response may be loaded within a browser frame.
Conclusion
The HTTP protocol is the backbone of communication via the Internet. It ensures that information reaches the intended recipient without any manipulation. By understanding its methods, headers, and status codes, you can appreciate how information is exchanged on the internet.



