HTTP requests must conform to the structure outlined by the World Wide Web Consortium (W3C). We'll discuss the most important and most commonly-used aspects of HTTP request structure.
A generic HTTP request looks like this:
GET /blog/ HTTP/1.1 Host: www.launchcode.org User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:67.0) Gecko/20100101 Firefox/67.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate, br DNT: 1 Connection: keep-alive Upgrade-Insecure-Requests: 1 Cache-Control: max-age=0 Request Body
The structure has these components:
The request line is minimal. We have already discussed the path, which specifies the resource being requested. The first part of the request line, the request method, is new to us.
A request method specifies the type of action to be carried out on the requested resource. HTTP defines 8 methods, of which we will only need to use 2:
GET method tells the server that we want to simply retrieve the resource. This is the most commonly used method. It is used for requests for HTML pages, CSS and JS files, and images. When you click on a link in a web page, you are triggering a
GET request for the linked page.
GET requests generally do not have a body, since they are asking rather than sending for information.
GET requests are usually used for:
POST method tells the server that we want to create new data on the server. As you will learn in the next chapter,
POST is used when submitting a form.
POST requests usually have a body, which contains data that the server processes and stores in some fashion.
Some common situations that use
There are quite a few request headers, but only a few will be useful to us.
||The domain name or IP address of the server the request should be sent to.||
||Information about the client (usually a browser) making the request. The example is for a version of Firefox on a Mac.||
||The types of data that the client is willing to accept in the response body.||
||The type of data included in the request body. Usually only used for
The optional request body may contain any data whatsoever, though it often includes form data submitted via a
POST request. For example, when signing into a web site, the request body will contain your username and password. We will later learn that it can contain other data formats such as XML and JSON.
As mentioned above,
GET requests generally do not have a body.