Mark As Completed Discussion

Let's learn about client-server interaction over HTTP in more detail:

  1. The client sends to the server a request that is basically a text document that consists of:
    • A verb (such as GET or POST ) that defines what action the server should perform,
    • Headers that define additional information about the request,
    • (Optional) body.
  2. The server sends a response consisting of:
    • A status code,
    • Headers,
    • (Optional) body.

Now let's see what an HTTP request is like by deconstructing each of its 3 components:

  • A verb (a.k.a. method) in an HTTP request expresses an action that needs to be taken with a resource. Although there are 39 possible HTTP verbs, many of them are downright esoteric. Most often you'll see these 5 verbs used:
    • GET: request a resource.
    • POST: create a resource.
    • PUT: update a resource.
    • PATCH: update a part of a resource.
    • DELETE: delete a resource.
  • Request headers are metadata about a request written as key-value pairs. Some headers can apply both to requests and responses while others are only used with requests. The most popular HTTP headers are:
    • Content-Type: indicates the media type of a resource.
    • Content-Length: indicates the size of request body.
    • Authorization: proves to the server that the requesting client is authorized to make the request.
    • Accept: specifies what content types the requesting client understands.
    • Cookie: contains HTTP cookies previously received from the server. Cookies are small pieces of data that are used to carry state across HTTP requests that are otherwise stateless.
  • An optional body of a request contains data associated with a request, such as the content of an HTML form passed with a POST request. However, content is invalid with some verbs, most notably with GET.

Any HTML response consists of the following parts:

  • Status code that represents the result of processing a request. Status code is a number that is interpreted depending on what range if falls into:
    • 100-199: informational status. Rarely used.
    • 200-299: success status. Any status in this range confirms, in one way or another, that a request has been successfully executed.
    • 300-399: redirection status. Status in this range mean that the requested resource needs to be found somewhere else, such as in a cache or at a different URL.
    • 400-499: client errors. Status in this range signal some kind of error by the client: for example, errors in the query string, absent query string, or a nonexistent URL.
    • 500-599: server errors report that something went wrong on the server, and it's not the client's fault.
  • Response headers are metadata about a response. They're similar to request headers, and in fact, a lot of headers can be used both in requests and in responses. Some response-specific headers include:
    • Expires: sets the date/time after which the response is considered outdated.
    • Set-Cookie: used to send a cookie from the server to the client, so that the client can send the cookie back to the server later.
  • An optional body. A normal HTML page references CSS, JavaScript, XML, JSON, images and other kinds of resources, each requiring its own HTTP request.

Request and response are two individual calls. On the HTTP level, there's no connection between the client and the server that lasts. The responding web server is stateless: it's going to forget about the requester the moment it sends a response.