Module 16 · External Libraries and APIs — topic 10 of 12
HTTP requests
The HTTP protocol for programmers: methods, URLs, headers and bodies, status code classes, content types, and sending GET and POST requests with urllib — including handling error responses.
Learning objectives
- Describe the parts of an HTTP request and response
- Interpret status codes
- Send GET and POST requests and handle errors with urllib
Definition
HTTP is the protocol of the web. A client sends a request (method, URL, headers and optional body) and the server returns a response (status code, headers and body).
Introduction
Every web page load, API call and form submission is an HTTP request. Knowing its parts makes debugging network code much easier.
Why it matters
Status codes and headers tell you exactly why a request failed — if you know how to read them.
Detailed explanation
Request parts: method (GET, POST, PUT, PATCH, DELETE), URL, headers (metadata such as Content-Type, Authorization, User-Agent) and an optional body.
Response parts: status code, headers and body.
Status classes: 2xx success (200 OK, 201 Created), 3xx redirection, 4xx client errors (400 Bad Request, 401 Unauthorized, 404 Not Found, 429 Too Many Requests), 5xx server errors.
GET requests should not change data and carry parameters in the URL; POST sends data in the body, usually JSON with Content-Type: application/json.
urllib.request.urlopen sends requests; a Request object sets the method, headers and body. urlopen raises HTTPError for 4xx/5xx responses — the error object still has the code and body.
Network failures (no connection, DNS problems) raise URLError; slow servers trigger a timeout if you pass timeout=.
http.HTTPStatus maps status codes to their standard phrases.
Syntax
- Requests have method, URL, headers, body.
- Status codes classify outcomes.
- HTTPError still carries the response.
Code examples
Status codes
200 OK 201 Created 301 Moved Permanently 404 Not Found 429 Too Many Requests 500 Internal Server Error
Building a request
POST https://api.example.com/v1/users
[('Content-type', 'application/json')]More examples
GET, POST and an error response
200 {'query': 'python books', 'page': 2}
201 {'created': 'Asha', 'agent': 'learn-python-demo/1.0'}
404 {'error': 'no route /nothing'}Step-by-step explanation
- The GET request carries its parameters in the encoded query string.
- The POST request sends a JSON body with custom headers; the server reads them and replies 201 Created.
- urlopen raises HTTPError for the 404, but the error object can still be read like a response to get the JSON error message.
Expected output
Running “GET, POST and an error response” prints exactly:
Practical applications
- Debugging failed API calls by reading status codes and error bodies.
- Sending form data or JSON to web services.
Best practices
- Set Content-Type for request bodies.
- Handle HTTPError and URLError separately.
- Always pass a timeout.
Common mistakes
Treating every non-200 as a crash
Avoid
Prefer
Error responses carry useful information about what went wrong.
Important notes
- Browser developer tools (Network tab) show every HTTP request a page makes — a great way to learn.
Key takeaways
- HTTP requests have a method, URL, headers and body.
- Status codes: 2xx success, 4xx client error, 5xx server error.
- urllib sends requests; HTTPError carries error responses.
Try it yourself
- 1Add a DELETE handler to the local server and send a DELETE request with urllib.
- 2Write a function that returns (status, json) for any URL, including error responses.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
Test yourself across the whole course in the Python practice bank.