The Structure and Components of HTTP Requests and Responses

Jake Lamb
3 min readFeb 1, 2019

So this post is going to be a bit different… Today, I’m going to skip right to the point and give you no-nonsense examples of the structure of HTTP Requests and Responses. When I started building my HTTP server from scratch I had a very difficult time finding a resource that I found helpful when I was beginning to write a parser for Requests and something that could build Responses which were compliant with the spec. Let’s dive in!

Requests

The components of an HTTP Request are as follows:

Start Line

Consists of four parts separated by spaces

  • Request method (GET, POST, DELETE, etc.)
  • Path (“/”, “/index.html”, “/user/fakeaccount”, etc.)
  • Protocol/Version (Typically just “HTTP/1.1”)
  • Carriage return new line feed

Structure:

METHOD Space PATH Space HTTP/1.1 \r\n

Example:

GET / HTTP/1.1\r\n

This example would be making a GET request to the root of wherever the request was sent.

Something to note: If a Path is not specified in the Request there will be two spaces between the Method and the Protocol/Version.

Header/s

Consists of two parts

  • A case insensitive header name followed by a colon
  • A value with a carriage new line feed directly after

Structure:

NAME Colon Space VALUE \r\n

Example:

Host: google.com\r\n

Something to note: A typical request contains multiple headers in a list which would look like this.

accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\n
accept-encoding: gzip, deflate, br\r\n
accept-language: en-US,en;q=0.9\r\n
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36\r\n

Carriage Return New Line Feed

Literally just \r\n. This signals the end of the headers and the start of the body

Example:

\r\n

Body

Not all requests contain a body. If a Request contains a Body it will have a content-length header with a value defining how long the body is in bytes.

A body is going to be a raw version of whatever is being sent whether it’s HTML, JS, an image, etc. A content-type header should exist in the request to let the receiver know how to deal with the body content.

Structure:

Raw Data

Example:

{
"email": "typicalmilenial94@gmail.com",
"username": "AwfulUsername",
"password": "TrulyBadPassword"
}

Full Example

POST /v1/account/create HTTP/1.1\r\n 
X-Forwarded-Proto: https\r\n
Host: api.trendystartup.io\r\n
Content-Length: 106\r\n
Accept: */*\r\n
Accept-Encoding: gzip, deflate\r\n
Cache-Control: no-cache\r\n
Content-Type: application/json\r\n
User-Agent: PostmanRuntime/7.4.0\r\n
X-Forwarded-Port: 443\r\n
\r\n
{
"email": "typicalmilenial94@gmail.com",
"username": "AwfulUsername",
"password": "TrulyBadPassword"
}

Responses

The only difference between the structure of a Request and a Response is that instead of a Start Line as the first line, a Status Line is used instead (the specific headers will most likely be different as well). So let's take a look at what a Status Line looks like.

Status Line

Consists of four parts separated by spaces

  • Protocol/Version (Typically just “HTTP/1.1”)
  • Status Code (200, 400, 500, etc.)
  • Status Text (“OK”, “Bad Request.”, “Internal Server Error.”, etc.)
  • Carriage return new line feed

Structure:

HTTP/1.1 Space STATUS CODE Space STATUS TEXT \r\n

Example:

HTTP/1.1 200 OK\r\n

Full Example

This is an abridged response of what you get when you send a GET Request to https://www.google.com/

HTTP/1.1 200 OK\r\n
alt-svc: quic=":443"; ma=2592000; v="44,43,39"\r\n
cache-control: private, max-age=0\r\n
content-encoding: br\r\n
content-length: 70968\r\n
content-type: text/html; charset=UTF-8\r\n
date: Fri, 01 Feb 2019 04:22:44 GMT\r\n
expires: -1\r\n
p3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."\r\n
server: gws\r\n
status: 200\r\n
strict-transport-security: max-age=31536000\r\n
x-frame-options: SAMEORIGIN\r\n
x-xss-protection: 1; mode=block\r\n
\r\n
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Google</title>
</head>
<body>
<p>There was way too much HTML to include it all in this example...</p>
<p>I think you get the point.</p>
</body>
</html>

Ok, that’s all I have for you today! I hope you were able to get something out of this and hopefully gain a better understanding of the structure and components of HTTP Requests and Responses! If you have any feedback or would like to make a correction to anything in this post please leave a comment below! Thanks for reading!

--

--