HTTP Basics & urllib
HTTP (HyperText Transfer Protocol) is the foundation of data communication on the web. Python's standard library includes urllib for making HTTP requests without any third-party dependencies.
HTTP Methods
Each HTTP request uses a method (also called a verb) that describes the intended action:
| Method | Purpose | Has Body? |
|---|---|---|
| GET | Retrieve a resource | No |
| POST | Create a new resource | Yes |
| PUT | Replace a resource entirely | Yes |
| PATCH | Partially update a resource | Yes |
| DELETE | Delete a resource | No |
REST APIs follow these conventions:
GET /users→ list all usersPOST /users→ create a new userGET /users/42→ get user 42PUT /users/42→ replace user 42PATCH /users/42→ update specific fields of user 42DELETE /users/42→ delete user 42
HTTP Status Codes
The server always responds with a status code indicating success or failure:
2xx — Success
200 OK— request succeeded, body contains the result201 Created— resource was successfully created (often after POST)204 No Content— succeeded but no body (often after DELETE)
3xx — Redirection
301 Moved Permanently— resource has a new permanent URL302 Found— temporary redirect
4xx — Client Errors (your fault)
400 Bad Request— malformed request syntax or invalid parameters401 Unauthorized— authentication required or failed403 Forbidden— authenticated but not permitted404 Not Found— resource doesn't exist422 Unprocessable Entity— valid syntax but semantic errors in body429 Too Many Requests— rate limit exceeded
5xx — Server Errors (server's fault)
500 Internal Server Error— unexpected server-side crash502 Bad Gateway— upstream server failure503 Service Unavailable— server overloaded or in maintenance
Making a Simple GET Request
urllib.request.urlopen opens a URL and returns a response object:
The response object implements a context manager — always use with to ensure the connection is closed.
Reading JSON Responses
Most modern APIs return JSON. Use the json module to parse it:
Building Query Parameters with urllib.parse
Never manually concatenate query strings — use urllib.parse.urlencode:
Use urllib.parse.quote to percent-encode a single string value (e.g., a search term with spaces or special characters):
Custom Headers with urllib.request.Request
Use urllib.request.Request to add custom headers, change the method, or add a request body:
POST Requests with a JSON Body
Error Handling
urllib.error.HTTPError is raised for 4xx and 5xx responses. urllib.error.URLError is raised for network-level failures (DNS, connection refused, timeout):
Setting Timeouts
Prevent your program from hanging indefinitely by always specifying a timeout:
A Complete Example
Knowledge Check
Which HTTP status code indicates that a resource was successfully created by a POST request?
What is the correct way to build query parameters for a URL in Python's standard library?
What exception does urllib raise when the server responds with a 404 status code?