The HTTP message format
Requests and responses in HTTP are always sent as plain text messages. Those plain text messages consist of a well-ordered, and well-structured, series of message segments that can be reliably parsed by the recipient. In requests, messages consist of three required message components and one optional component:
- The request line consists of the method, the path to the requested resource, and the specific protocol version that should be used to determine the validity of the rest of the message; for example, GET /users/id/12 HTTP/1.1.
- A series of request headers and their values, for example, Accept: application/json.
- An empty line.
- (Optional) A request message body. This consists of content headers that provide metadata about the content type, as well as the content itself.
Each segment is delineated by a <CR> carriage return character and an <LF> line feed character; these are special white-space characters whose specific American Standard Code for Information Interchange (ASCII) values allow them to reliably be used to indicate the breaks between segments in a message stream.
Meanwhile, an HTTP response consists of its own series of almost identically structured segments, each also delimited by the <CR><LF> characters. Just as with the request message, it contains three required segments and one optional message body segment, as follows:
- A status line consisting of the specific protocol, the HTTP status code, and the reason phrase associated with that status code:
- HTTP/1.1 401 Bad Request: A response containing the 401 client error status code (indicating that the client sent an improper request message for the resource that it was looking for).
- HTTP/2.0 201 Created: A response indicating the 201 success status code, meaning that the desired resource has been created on the server.
- Headers, as with the request message segment, providing metadata about how the response should be parsed.
- An empty line.
- An optional message body.
Those simple segments fully define every valid HTTP message sent across the internet. This accounts for millions of requests per second, between millions or billions of devices. It's the simplicity of the message specification that makes that kind of scale possible.