A Basic Break Down of an HTTP Request
12 Dec 2012
Let's take a look at HTTP requests.
Since I started working on building a web server, it is inevitable that I will be dealing with the life blood of web servers, requests and responses. Today, let's go ahead and take a look at the HTTP request.
So often, we think nothing of sitting in front of our computers, tablets, phones, etc., typing in URLs, and getting what we asked for. It generally runs smoothly and can seem almost magical. Even if you have ever hit view source,(which if you are reading this blog I am going to assume that you have) you will not see everything that is going on behind the scenes. The magic beneath lies in HTTP requests and responses. Let's take a look at what one of these looks like and do a quick breakdown of the parts you need to know to get you started.
Exhibit A: Request Pseudocode
1 - A request line ex: GET / HTTP1.1 2 - Optional Headers (there can be several different types of these) ex: Content-Type: text/html 3 - An empty line 4 - An optional message body
Today, let's focus on that first line. Let's use the example that we already used.
1 2 GET / HTTP1.1
There are 3 parts to this request line. The three different parts are separated by spaces. The first part (GET in this case), is the method name. Method names tell you what kind of operation the client has requested. The second part is the path of the requested resource aka the request URI. The request uri could be as simple as / or something like /hello/1 or much longer than that. The third part is the HTTP version. The syntax is HTTP/version-number.
That is it for the first line.
Pretty neat, don't you think?