Paul's Python CoursePPC

Requests and responses

Not met yet

A web request has a method (GET asks, POST sends something new, DELETE removes), a path such as /api/bookings/3, perhaps a query string after the ?, and perhaps a body. The response has a status code and a body. The status's first digit says how it went: 2 for success (200 here it is, 201 made, 204 done with nothing to add), 4 for a problem with the request (400 sent wrong, 404 not there, 409 clashes with something), 5 for a problem in the server. Everything in a URL arrives as text, numbers included.

Try it

main.py
from http import HTTPStatus
from urllib.parse import parse_qs, urlsplit

url = urlsplit("/api/bookings?date=2027-04-11&desk=2")
print(url.path)
print(parse_qs(url.query))
print(409, HTTPStatus(409).phrase)

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks a value in a URL's query string, such as berth=2, arrives as a number. Everything in a URL is text.
  • Thinks a response can say 200 and put the problem in the body. The status code is how programs know it went wrong.

Where it comes up