Anatomy of an HTTP request
Translations

HTTP, for Hypertext Transfer Protocol, is the main protocol for Internet exchanges. It’s used by the browser you’re using to read this article, as well as by other applications. It’s based on an exchange of requests and responses, between a client and a server, in text format. The advantage of text format is that it’s easy to implement in any programming language. The HTTP protocol is specified in RFC 2616, the very first version of which dates back to 1990.
This article deals with version 1.1 of the protocol, which was standardized in 1997. A version 2.0 was standardized in 2015, and extends version 1.1 with additional capabilities. The basics of version 1.1 are therefore not obsolete!
In this article, we’ll look at the structure of an HTTP request and response.
The structure of a request
A request is structured in 3 parts:
- the mandatory first line of the request
- the optional headers
- the request body, also optional
REQUEST-LINE
[HEADERS]
[BODY]
Example of an HTTP GET request without a request body:
GET /articles/http-anatomy?version=1#headers HTTP/1.1
Host: codeka.io
Accept: text/plain
Accept-Charset: utf-8
Accept-Encoding: gzip
Accept-Language: fr-fr
Example with a request body :
POST /articles/ HTTP/1.1
Host: codeka.io
Content-Type: application/json
Content-Length: 31
Authorization: Basic UmljayBBc3RsZXk6TmV2ZXIgR29ubmEgR2l2ZSBZb3UgVXAK
{"title":"my beautiful article"}
The Request Line
The HEADERS and BODY are optional.
The REQUEST-LINE is defined in this way:
METHOD REQUEST-URI HTTP-VERSION
METHOD is the HTTP verb. It can be GET
, POST
, PUT
, DELETE
, which are the most common HTTP verbs.
RFC 2616 also defines additional methods, such as HEAD
, OPTIONS
, TRACE
, CONNECT
.
REQUEST-URI is the resource to be called. It can be an absolute URI, like https://codeka.io/articles/http-anatomy
, or a relative URI, like /articles/http-anatomy
. In practice, relative URIs are used in combination with a Header Host
.
The HTTP-VERSION
field is set to HTTP/1.1
.
A minimal HTTP request is :
GET /articles/http-anatomy HTTP/1.1
URL and URI
A URI, for Uniform Resource Identifier, defines an Internet resource. This resource can be a web page, a document in PDF format, or a video.
A URL, for Uniform Resource Locator, is a particular URI, which contains the network information needed to access a resource. URIs are defined by a text format made up of ASCII characters.
RFC 3986 specifies URI formats.
The structure of an absolute URI is as follows:
SCHEME://HOST/PATH[?QUERY][#FRAGMENT]
The SCHEME is often associated with the protocol, http, https, or file when local files are designated.
The HOST is the name of the server hosting the resource.
The PATH identifies the path to the resource.
The QUERY, also often called Query Strings, defines additional parameters that the resource can process, in the form name=value
.
The FRAGMENT defines the part of the resource consulted.
QUERY and FRAGMENT are optional.
A simple URI for our article is :
https://codeka.io/articles/http-anatomy
\___/ \_______/ \___________________/
| | |
scheme host path
A URI with additional elements :
https://codeka.io/articles/http-anatomy?version=1#headers
\___/ \_______/ \___________________/ \_______/ \_____/
| | | | |
scheme host path query fragment
A URI can also be relative, and contain only part of the PATH. For instance :
/http-anatomy?version=1#headers
Request _Headers
Headers define additional information about the request, such as the data format expected in response, or information about data currently cached on the client side.
This simple structure defines the Headers:
NAME: VALUE
RFC 2616, defines standard Headers, but applications can also define their own Headers.
The main standard Headers are Accept
, Accept-Charset
, Accept-Encoding
and Accept-Language
for content negotiation.
This RFC also defines the Header Authorization
for client authentication. The Header Host
specifies the host to which the request is sent if it is not specified in the request URI.
An HTTP request containing Headers is :
GET /articles/http-anatomy HTTP/1.1
Host: codeka.io
Accept: text/plain
Accept-Charset: utf-8
Accept-Encoding: gzip
Accept-Language: fr-fr
The request Body
When a request includes a body, the Headers Content-Type
and Content-Length
must be specified, defining its type and size in bytes.
A line break must also separate the Headers from the Body.
Example of a query with a Body in JSON
format:
POST /articles/ HTTP/1.1
Host: codeka.io
Content-Type: application/json
Content-Length: 31
{"title":"my beautiful article"}
The structure of a response
HTTP responses have a format quite similar to that of requests.
STATUS-LINE
[HEADERS]
[BODY]
Status Line
The Status Line represents the server’s coded response to the request.
This is how STATUS-LINE is defined:
HTTP-VERSION CODE PHRASE
The first element of the response is the HTTP version HTTP/1.1
.
The codes used are represented by 3 digits.
1xx
codes are informational and rarely used in practice.2xx
codes represent a successful operation.3xx
codes indicate a redirection.4xx
codes indicate a client error (malformed request, non-existent resource).5xx
codes represent a server error in processing the request.
Codes are followed by a descriptive Phrase.
The most common codes and associated phrases are defined in RFC 2616.
Example of an error response:
HTTP/1.1 404 Not Found
An example of a successful response:
HTTP/1.1 200 OK
Response _Headers
Like a request, a response can contain Headers. They define the type and size of the response Body, or parameters to indicate to the client that the response can be cached. Other Headers define the resource to be called in the event of a redirection (code 3xx
).
Response Headers are defined in the same way as requests:
NAME: VALUE
Example of an HTTP response including a Header Location
for a redirect to an index.html
page:
HTTP/1.1 301 Moved Permanently
Location: index.html
The response Body
The Body is defined in the same way as requests, and also imposes the same Headers Content-Type
and Content-Length
rules.
Example of a successful response with the content of our article :
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 31
{"title":"my beautiful article"}
Example of a server error, with an HTML page representing it:
HTTP/1.1 500 Internal Server Error
Content-Type: text/html
Content-Length: 56
<html><body><h1>Server Error</h1></body></html>
Conclusion
The HTTP protocol describes a text format, in the form of a request and response, which is easy to implement in any programming language. Its simplicity and expressiveness have been the key to its success since the 90s, with new versions continuing to use the same format.
Références
- RFC 2616Â : Hypertext Transfer Protocol – HTTP/1.1
- RFC 3986Â : Uniform Resource Identifier (URI) : Generic Syntax
- RFC 7540Â : Hypertext Transfer Protocol Version 2 (HTTP/2)
- Cover photo by Jordan Harrison on Unsplash