HTTP Request Format3:01 with Chris Ramacciotti
Now that you've seen HTTP in action, it's time to cover the details of both the request and the subsequent response. In this video, we cover the format of the request. In general, there are four parts: (1) the request line, (2) a series of headers with name/value pairs on separate lines, (3) a blank line, and (4) if the request is a POST request, a body or payload.
HTTP 1.1 Specifications
Now that you've seen a few sample HTTP requests using telnet, 0:00 I'd like to show you the overall format of an HTTP request 0:04 as stated in the specifications which I've linked to in the teacher's notes. 0:08 Here's how an HTTP request needs to look. 0:13 HTTP request begin with request line. 0:17 This starts with what's called the request method, get or post. 0:20 Know that there are other request methods available on HTTP. 0:25 They're outside the scope of this course but 0:28 I've linked to them in case you're curious. 0:29 In general the get request method is used when you want to 0:32 view a resource on the server without making any changes. 0:36 And post request are used for actually making a change or 0:40 update to the listed resource. 0:44 We'll look at post request later in the course. 0:47 Speaking of the resource, that's what's listed next, the URI, or 0:50 again, Uniform Resource Identifier. 0:54 If you want to view the root, or 0:58 what's typically the home page, you'll use a single slash here. 0:59 Finally, HTTP is listed as the protocol in use followed by a slash and 1:04 ending with the version of HTTP, currently 1.1. 1:09 As a side note and heads up, HTTP/2 is on the way. 1:13 Don't worry though, 1:17 HTTP/2 won't change any of the stuff we're discussing in this course. 1:17 And you'll be able to continue using HTTP/1.1 likely for many years to come. 1:22 For more info on HTTP/2, check the teacher's notes. 1:28 The next line begins with what's called the header section of the request. 1:32 This communicates information related to the request being made, and 1:35 can include all sorts of info. 1:39 In this example, you see it includes the host which we've discussed. 1:42 The user agent which identifies the exact device making the request 1:45 I've listed telnet here and 1:49 accept language which tells the server which language we prefer for the response. 1:51 There are lots of other headers available and 1:57 we'll see a bunch more throughout the best of the course. 1:59 Following the headers is a blank line. 2:02 Then concluding the request, is the request body, or 2:04 what's frequently called the payload. 2:08 If the headers refer to the information related to the request, 2:10 the payload refers to the data being transferred in the request, if any. 2:14 As we'll see, data needs to be transferred when the purpose of the request 2:19 is to change a resource on the server. 2:23 For example, consider the scenario of using a form to 2:25 edit the first name of a contact saved in your address book online. 2:28 When you submit those changes, 2:33 a post request is sent containing the new first name in the payload. 2:35 In general, the exact changes to be made on the server 2:41 will be contained in the payload of the post request. 2:44 You won't see a payload in the get request. 2:48 And there you have it, the format for any HTTP request. 2:51 With that said, 2:55 let's turn to the other side of this web conversation, the response. 2:57
You need to sign up for Treehouse in order to download course files.Sign up