19.4. Requests

All HTTP requests must follow the structure described by the World Wide Web Consortium (W3C). Let’s take a look at the most important and most commonly used parts of this structure.

In general, an 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.15; rv:67.0) Gecko/20100101 Firefox/82.0.2
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:

  1. Request line: The first line is the request line. It includes the request method, the path from the URL, and the HTTP version being used.

  2. Request headers: From line 2 until the first blank line, the headers consist of a series of key-value pairs, one per line.

  3. Blank line: This marks the end of the request headers.

  4. Request body (Optional): Below the blank line, the request body takes up the remainder of the HTTP request.

19.4.1. Request Methods

In the request line, we already discussed the role the path plays in the URL. However, the first part of this line is new to us.

The request method identifies the action that must be done with the requested data. HTTP defines 8 methods, but we only need to pay attention to two of them: GET and POST.

19.4.1.1. The GET Method

The GET method tells the server that we only want to retrieve data. This is the most commonly used method. It is used to request HTML pages, CSS and JavaScript files, and images. When we click on a link in a web page, we trigger a GET request for the linked page.

GET requests usually do not have a body, since they just ask for information. We don’t need to provide commands to change that data in any way.

Example

GET requests are usually used for:

  1. Loading a page after typing its URL in the browser’s address bar.

  2. Conducting a search via a search engine.

  3. Loading a page after clicking on a link.

  4. Refreshing a webpage.

19.4.1.2. The POST Method

Using the POST method tells the server that we want to add or modify the data on the server. As we will learn in the next chapter, POST is used when submitting a form.

POST requests usually have a body. It contains data that the server will process and/or store in some fashion.

Example

POST requests are used for:

  1. Logging into a web site.

  2. Sending an email using an online service.

  3. Making an online purchase.

  4. Providing information through an electronic form.

19.4.2. Headers

There are lots of request headers, but only a few will be useful to us.

Common HTTP Request Headers

Header

Purpose

Example

Host

The domain name or IP address for the server receiving the request.

www.launchcode.org

User-Agent

Information about the client (usually a browser) making the request. The example is for a version of Firefox on a Mac.

Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:67.0) Gecko/20100101 Firefox/82.0.2

Accept

The types of data that the client requires in the response.

text/html, image/jpeg

Content-Type

The type of data included in the request body. Usually only used for POST requests.

application/json, application/xml

19.4.3. Body

The optional request body can contain any kind of data. For example, when signing into a web site, the body will contain a username and password.

As mentioned above, GET requests generally do not have a body.

19.4.4. Check Your Understanding

Question

Which type of method (GET or POST) should be used for each of the following actions. Click on each option to reveal the correct answer.

  1. Making a purchase from Amazon.
  2. Logging into a gmail account.
  3. Looking up (then reading) an NPR article.
  4. Streaming a movie trailer on YouTube.
  5. Clicking a link in a Wikipedia page.
  6. Submitting an online assignment to your teacher.