← All lessons
DNS, HTTP and other protocols · Lesson 3 of 7

URLs and HTTP

A URL says exactly what to fetch and how. In https://quackgames.net:443/learn?x=1#top, https is the scheme (the protocol), quackgames.net is the host, 443 is the port (usually left out, since it's the default for https), /learn is the path, x=1 is the query, and top is the fragment, which the browser keeps to itself to jump to part of the page.

from urllib.parse import urlsplit
u = urlsplit("https://quackgames.net:443/learn?x=1#top")
print(u.scheme, u.hostname, u.port, u.path, u.query, u.fragment)
Prints
https quackgames.net 443 /learn x=1 top

HTTP (Hypertext Transfer Protocol) is how the browser asks for things. A request is a method, a path and some headers: lines of extra information, like which host it's for, what languages you read, and any cookies.

GET /learn HTTP/1.1
Host: quackgames.net
Accept-Language: en-GB
User-Agent: Mozilla/5.0 …

The response starts with a status line, then headers, then the body (the HTML, an image, some JSON). Content-Type says what the body is.

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 5123

<!doctype html>…

The method says what you want. GET reads something and changes nothing. POST sends data, like a form. PUT replaces something, PATCH changes part of it, and DELETE removes it. Newer versions (HTTP/2 and HTTP/3) send the same requests more efficiently, several at once over one connection, but the meaning hasn't changed.

Practice

In https://example.com/shop/cart?id=7, what's the path?

More info

Quick reference

URLscheme://host:port/path?query#fragment
RequestMethod, path, headers (and sometimes a body).
ResponseStatus line, headers, body.
GET / POSTRead / send data.
PUT / PATCH / DELETEReplace / change part / remove.
Content-TypeWhat the body is: text/html, application/json.

The other networking courses