···11+---
22+created: "2023-05-25"
33+date: "2023-05-25T00:00:00Z"
44+language: fr
55+modified: "2023-07-01"
66+tags:
77+- Basics
88+title: Anatomy of an HTTP request
99+slug: anatomie-requete-http
1010+---
1111+1212+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.
1313+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.
1414+The HTTP protocol is specified in [RFC 2616](https://www.rfc-editor.org/rfc/rfc2616), the very first version of which dates back to 1990.
1515+1616+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!
1717+1818+
1919+2020+In this article, we'll look at the structure of an HTTP request and response.
2121+2222+## The structure of a request
2323+2424+A request is structured in 3 parts:
2525+2626+* the mandatory first line of the request
2727+* the optional headers
2828+* the request body, also optional
2929+3030+```
3131+REQUEST-LINE
3232+[HEADERS]
3333+3434+[BODY]
3535+```
3636+3737+Example of an HTTP _GET_ request without a request body:
3838+3939+```
4040+GET /articles/http-anatomy?version=1#headers HTTP/1.1
4141+Host: codeka.io
4242+Accept: text/plain
4343+Accept-Charset: utf-8
4444+Accept-Encoding: gzip
4545+Accept-Language: fr-fr
4646+```
4747+4848+Example with a request body :
4949+5050+```
5151+POST /articles/ HTTP/1.1
5252+Host: codeka.io
5353+Content-Type: application/json
5454+Content-Length: 31
5555+Authorization: Basic UmljayBBc3RsZXk6TmV2ZXIgR29ubmEgR2l2ZSBZb3UgVXAK
5656+5757+{"title":"my beautiful article"}
5858+```
5959+6060+### The _Request Line_
6161+6262+The _HEADERS_ and _BODY_ are optional.
6363+6464+The _REQUEST-LINE_ is defined in this way:
6565+6666+```
6767+METHOD REQUEST-URI HTTP-VERSION
6868+```
6969+7070+_METHOD_ is the HTTP verb. It can be `GET`, `POST`, `PUT`, `DELETE`, which are the most common HTTP verbs.
7171+RFC 2616 also defines additional methods, such as `HEAD`, `OPTIONS`, `TRACE`, `CONNECT`.
7272+7373+_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`.
7474+7575+The `HTTP-VERSION` field is set to `HTTP/1.1`.
7676+7777+A minimal HTTP request is :
7878+7979+```
8080+GET /articles/http-anatomy HTTP/1.1
8181+```
8282+8383+### _URL_ and _URI_
8484+8585+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.
8686+8787+A _URL_, for _Uniform Resource Locator_, is a particular _URI_, which contains the network information needed to access a resource.
8888+_URIs_ are defined by a text format made up of _ASCII_ characters.
8989+9090+[RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) specifies _URI_ formats.
9191+9292+The structure of an absolute _URI_ is as follows:
9393+9494+```
9595+SCHEME://HOST/PATH[?QUERY][#FRAGMENT]
9696+```
9797+The _SCHEME_ is often associated with the protocol, _http_, _https_, or _file_ when local files are designated.
9898+9999+The _HOST_ is the name of the server hosting the resource.
100100+101101+The _PATH_ identifies the path to the resource.
102102+103103+The _QUERY_, also often called _Query Strings_, defines additional parameters that the resource can process, in the form `name=value`.
104104+105105+The _FRAGMENT_ defines the part of the resource consulted.
106106+107107+_QUERY_ and _FRAGMENT_ are optional.
108108+109109+A simple _URI_ for our article is :
110110+111111+```
112112+https://codeka.io/articles/http-anatomy
113113+\___/ \_______/ \___________________/
114114+ | | |
115115+scheme host path
116116+```
117117+118118+A _URI_ with additional elements :
119119+120120+```
121121+https://codeka.io/articles/http-anatomy?version=1#headers
122122+\___/ \_______/ \___________________/ \_______/ \_____/
123123+ | | | | |
124124+scheme host path query fragment
125125+```
126126+127127+A _URI_ can also be relative, and contain only part of the _PATH_.
128128+For instance :
129129+130130+```
131131+/http-anatomy?version=1#headers
132132+```
133133+134134+### Request _Headers
135135+136136+_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.
137137+138138+This simple structure defines the _Headers_:
139139+140140+```
141141+NAME: VALUE
142142+```
143143+144144+[RFC 2616](https://www.rfc-editor.org/rfc/rfc2616#page-38), defines standard _Headers_, but applications can also define their own _Headers_.
145145+The main standard _Headers_ are `Accept`, `Accept-Charset`, `Accept-Encoding` and `Accept-Language` for content negotiation.
146146+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.
147147+148148+An HTTP request containing _Headers_ is :
149149+150150+```
151151+GET /articles/http-anatomy HTTP/1.1
152152+Host: codeka.io
153153+Accept: text/plain
154154+Accept-Charset: utf-8
155155+Accept-Encoding: gzip
156156+Accept-Language: fr-fr
157157+```
158158+159159+### The request _Body_
160160+161161+When a request includes a body, the _Headers_ `Content-Type` and `Content-Length` must be specified, defining its type and size in bytes.
162162+A line break must also separate the _Headers_ from the _Body_.
163163+164164+Example of a query with a _Body_ in `JSON` format:
165165+166166+```
167167+POST /articles/ HTTP/1.1
168168+Host: codeka.io
169169+Content-Type: application/json
170170+Content-Length: 31
171171+172172+{"title":"my beautiful article"}
173173+```
174174+## The structure of a response
175175+176176+HTTP responses have a format quite similar to that of requests.
177177+178178+```
179179+STATUS-LINE
180180+[HEADERS]
181181+182182+[BODY]
183183+```
184184+185185+### _Status Line_
186186+187187+The _Status Line_ represents the server's coded response to the request.
188188+189189+This is how _STATUS-LINE_ is defined:
190190+191191+```
192192+HTTP-VERSION CODE PHRASE
193193+```
194194+195195+The first element of the response is the HTTP version `HTTP/1.1`.
196196+197197+The codes used are represented by 3 digits.
198198+199199+* `1xx` codes are informational and rarely used in practice.
200200+* `2xx` codes represent a successful operation.
201201+* `3xx` codes indicate a redirection.
202202+* `4xx` codes indicate a client error (malformed request, non-existent resource).
203203+* `5xx` codes represent a server error in processing the request.
204204+205205+Codes are followed by a descriptive _Phrase_.
206206+207207+The most common codes and associated phrases are defined in [RFC 2616](https://www.rfc-editor.org/rfc/rfc2616#section-6.1.1).
208208+209209+Example of an error response:
210210+211211+```
212212+HTTP/1.1 404 Not Found
213213+```
214214+215215+An example of a successful response:
216216+217217+```
218218+HTTP/1.1 200 OK
219219+```
220220+221221+### Response _Headers
222222+223223+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`).
224224+225225+Response _Headers_ are defined in the same way as requests:
226226+227227+```
228228+NAME: VALUE
229229+```
230230+231231+Example of an HTTP response including a _Header_ `Location` for a redirect to an `index.html` page:
232232+233233+```
234234+HTTP/1.1 301 Moved Permanently
235235+Location: index.html
236236+```
237237+238238+### The response _Body_
239239+240240+The _Body_ is defined in the same way as requests, and also imposes the same _Headers_ `Content-Type` and `Content-Length` rules.
241241+242242+Example of a successful response with the content of our article :
243243+244244+```
245245+HTTP/1.1 200 OK
246246+Content-Type: application/json
247247+Content-Length: 31
248248+249249+{"title":"my beautiful article"}
250250+```
251251+252252+Example of a server error, with an HTML page representing it:
253253+254254+```
255255+HTTP/1.1 500 Internal Server Error
256256+Content-Type: text/html
257257+Content-Length: 56
258258+259259+<html><body><h1>Server Error</h1></body></html>
260260+```
261261+262262+## Conclusion
263263+264264+The HTTP protocol describes a text format, in the form of a request and response, which is easy to implement in any programming language.
265265+Its simplicity and expressiveness have been the key to its success since the 90s, with new versions continuing to use the same format.
266266+267267+## Références
268268+269269+* [RFC 2616](https://www.rfc-editor.org/rfc/rfc2616) : Hypertext Transfer Protocol -- HTTP/1.1
270270+* [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) : Uniform Resource Identifier (URI) : Generic Syntax
271271+* [RFC 7540](https://www.rfc-editor.org/rfc/rfc7540) : Hypertext Transfer Protocol Version 2 (HTTP/2)
272272+* Cover photo by [Jordan Harrison](https://unsplash.com/@jouwdan?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash) on [Unsplash](https://unsplash.com/photos/blue-utp-cord-40XgDxBfYXM?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash)