Requests and responses

Reading · 5 min · Module 6, lesson 1 of 421 min left in this module

Module 6 · HTTP and HTTPSLesson 1 of 4

Goal: Name the parts of an HTTP request and response, and map each part of a URL to where it's used.

4:00 · captions and chapters · narrated with an AI-generated voice
Transcript

Narration uses an AI-generated voice.

[00:00] One question, one answer

By the end of this video, you'll be able to name every part of an HTTP request, and of its response. And you'll know where each part of a web address is used.

Our request has now reached HTTP. HTTP is a conversation of one question, and one answer. The browser sends a request, saying what it wants. The server sends back a response. How it went, and usually, the content. Each exchange stands alone. The server doesn't remember you between requests.

[00:34] A URL, taken apart

Start with the address. It has four parts, each used at a different hop. First, the scheme, https. Encrypt the connection, on port four four three. Next, the host. It's looked up in DNS, then sent again inside the request. Then the path, slash cart. Which page on the site. And the query, after the question mark. Extra values for the server.

[01:02] The request

Now the request itself. Its first line has three parts. The method, POST. That's what you want to do. The path, slash cart. And the HTTP version. Then the headers. Each is a name and a value. The Host header names the site. Others describe the body, or send a cookie. A blank line ends the headers. Then comes the body, here the item for the cart.

[01:29] The Host header

That Host header matters more than it looks. Many sites can share one server, at one IP address. So the address alone can't say which site you meant. The Host header does. The server reads it, and picks the site.

And the method? GET fetches. POST sends data, like that cart item. The next lesson covers the rest.

[01:53] The response

The response has the same shape. First, the status line. The version, then the status, two oh one Created. Three digits that say how it went. Then its headers. Content-Type says the body is JSON. And Set-Cookie asks the browser to store a cookie. Then a blank line, and the body. So every request and response has one shape. A first line, headers, a blank line, then maybe a body. Name the parts, and you can read any of them.

[02:27] One page, many requests

Loading a page is rarely one exchange. The browser fetches the HTML, and reads it. Then it requests every stylesheet, script, image and font the page mentions. Often dozens. To watch them, open the Network tab in your browser's developer tools, and reload.

And since each exchange stands alone, the server can't tell your second request from a stranger's. Unless the request carries something that identifies you. Usually a cookie, which is coming up later in this module.

[03:01] See a real one

You can see a real response yourself. Point curl at a deployed service, asking for just the headers. The first line says HTTP two, two hundred. It answered over HTTP two, with success. Below it are the headers. This body is plain text. HTTP two sends the same parts in a binary format. You read them the same way.

[03:28] Recap

So, a quick recap. A request is a method, a path, headers, and maybe a body. A response is a status, headers, and maybe a body. And the Host header tells a shared server which site you meant.

Now try it. Open the Network tab, reload a page, and pick one request. Name its method, its path, and its status. I'll see you in the next lesson.

HTTP at a glance

Request
A method, a path, headers, maybe a body.
Response
A status, headers, maybe a body.
Status code
Three digits saying how it went.
Header
A name and a value.
Cookie
What lets the server recognise you.
The browser asks, the server answers, and a few named parts carry everything in between.

Key idea

HTTP is a conversation of one question and one answer. The browser sends a request saying what it wants; the server sends back a response saying how it went and, usually, the content. Each exchange stands alone: the server doesn't remember you from one request to the next unless something reminds it.

A URL, taken apart

https://shop.example.com/cart?coupon=SPRING has four parts, and each is used at a different hop:

  • https, the scheme: encrypt the connection (module 7), on port 443.
  • shop.example.com, the host: looked up in DNS, then sent again inside the request.
  • /cart, the path: which page or resource on that site.
  • ?coupon=SPRING, the query: extra values for the server to use.

The exchange

The Host header matters more than it looks. Many sites can share one IP address, and the Host header is how the server knows which one you meant.

The method says what you want to do. GET fetches and POST sends data; the next lesson covers the rest, and the status codes.

One page, many requests

Loading a page is rarely one exchange. The browser fetches the HTML, reads it, then makes a request for every stylesheet, script, image and font it mentions, often dozens. Open your browser's developer tools on the Network tab and reload any page to watch them.

"Stands alone" is why logins need help. The server can't tell your second request from a stranger's unless the request carries something that identifies you, usually a cookie (lesson 1.6.3).

HTTP/1.1, HTTP/2 and HTTP/3

The diagram shows HTTP/1.1, which is plain text. HTTP/2 and HTTP/3 carry the same parts (method, path, headers, status, body) in a binary format, and send many requests at once over one connection. What each part means doesn't change, so you read them the same way; the Host header just travels under the name :authority. Tools like curl show which version was used.

Check yourself

Two shops, a.example.com and b.example.com, run on one server with one IP address. How does the server know which shop a request is for?