19.5. Responses

Every time a web server receives an HTTP request, it sends an HTTP response back to the client.

A generic HTTP response looks like this:

HTTP/2.0 200 OK
Date: Wed, 22 May 2020 17:36:50 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 8050
Last-Modified: Wed, 22 May 2020 17:33:45 GMT

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!--Rest of HTML page -->
</html>

The structure has these components:

  1. Response line: The first line of the response contains status information like the response code. In this example, the response code is 200, which indicates the request was successfully fulfilled.

  2. Response headers: Below the response line are the response headers. Similar to request headers, these are key-value pairs that contain data about the response. In the example above, these include information about the date and time, the data being sent (text and HTML), the size of the response body, and when it was last modified.

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

  4. Response body (Optional): Below the blank line, the response body takes up the rest of the HTTP response. This usually includes the HTML and CSS for the requested webpage.

19.5.1. Response Codes

We’ve all gotten a 404 File Not Found error code at some point when we’ve surfed the web.

A 404 message from GitHub displayed in a browser.

Did we make a mistake when typing the URL?

While 404 is the most common error we see when clicking links or typing in URLs, other codes exist as well.

Servers use HTTP response codes to report the result when they try to answer a client’s request. The codes are always three-digit numbers, and they fall into one of five categories:

  1. Information: (1xx) The request was received, but the server is still working on the response.

  2. Success: (2xx) The request was valid and the server successfully responded.

  3. Redirect: (3xx) The request is incomplete. The client must take additional action, such as sending the request to a different server, before a response can be returned.

  4. Client Error: (4xx) There was a problem with the client’s request.

  5. Server Error: (5xx) The request was valid, but the server hit a problem when trying to respond to it.

Specific codes will have all 3 digits specified, such as 201 (request fulfilled), 303 (see other URL), or 403 (forbidden). Each code has a specific meaning. For example, a 404 response code means that the requested data does not exist on the server. This can occur if we make a mistake when typing a URL into the address bar. Going back to the post office model, imagine sending Aunt Linda’s invitation to the wrong address. The letter would be delivered to a house where she doesn’t live. The request arrives, but a response from her is impossible.

Tip

We don’t need to memorize all of the response codes, because we can always look them up when necessary!

List of HTTP Status Codes.

19.5.2. Response Headers

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

Common HTTP Response Headers

Header

Purpose

Example

Content-Type

The type of data included in the response body.

text/html, text/css, image/jpg

Content-Length

The size of the response body in bytes.

348

Location

The URL that the client should visit to find a relocated resource.

https://www.launchcode.org/new-blog/

19.5.3. Response Body

While requests often don’t have a body, responses almost always have one. The response body contains the data that the client requested. It can contain HTML, CSS, JavaScript, or image data.

When a browser receives a response from a server, it loads the body into its memory. For HTML files, the browser executes the code and renders it into a webpage. For CSS files, the style rules and applied to the given HTML page.

19.5.4. Check Your Understanding

Question

A 404 response indicates that:

  1. The server is offline.
  2. The user needs to log in first.
  3. The requested data does not exist on the server.
  4. The server crashed.

Question

Which of the following response codes might indicate that the server is down for maintenance?

  1. 102
  2. 204
  3. 303
  4. 403
  5. 503