HTTP Requests using requests
HTTP Requests Using the Requests Library in Python
In modern software development, applications rarely operate in isolation. They communicate with external web services, payment gateways, authentication providers, and cloud databases via HTTP (Hypertext Transfer Protocol). While Python includes a standard urllib module, the third-party requests library is the undisputed industry standard—dubbed "HTTP for Humans"—due to its intuitive, developer-friendly API.
1. The HTTP Request-Response Cycle
HTTP follows a classic client-server model:
- 1Client Request: Your Python script sends a message specifying a Method (
GET,POST), target URL, optional Headers (metadata), and optional Body payload. - 2Server Response: The remote server processes the request and responds with a Status Code, response headers, and body content (HTML, JSON, XML, or binary data).
Common HTTP Status Codes:
200 OK: Request succeeded.201 Created: Resource successfully created.400 Bad Request: Malformed client payload.401 Unauthorized: Authentication credentials missing or invalid.403 Forbidden: Authenticated, but lacking permission.404 Not Found: Target endpoint does not exist.500 Internal Server Error: Server-side crash.503 Service Unavailable: Server overloaded or undergoing maintenance.
2. Installing and Making Your First Request
Install the library using pip:
Making a basic GET request:
3. Key Attributes of the Response Object
| Attribute / Method | Description | Example |
|---|---|---|
response.status_code | Integer HTTP status code | 200, 404 |
response.ok | Boolean: True if status code < 400 | True |
response.text | Decoded string content (uses encoding) | "{\"title\": \"...\"}" |
response.content | Raw binary bytes (for images/files) | b'\x89PNG...' |
response.headers | Case-insensitive dictionary of HTTP headers | {'Date': '...', ...} |
response.url | Final URL after any redirects | "https://..." |
response.raise_for_status() | Raises HTTPError if status is 4xx or 5xx | Exception or None |
4. Setting Timeouts: A Critical Best Practice
By default, requests does not time out. If an external server freezes or hangs, your Python script will wait indefinitely, blocking execution and consuming server threads.
Always pass the timeout parameter (in seconds):
5. Comprehensive Error Handling with requests.exceptions
All exceptions in the requests library inherit from requests.exceptions.RequestException. Handle network errors defensively:
Multiple Choice Questions
1. What does the response.ok property return when a server responds with status code 200?
A. 200 B. True C. "OK" D. None Answer: B Explanation: response.ok returns True if the HTTP status code is less than 400 (i.e. successful or redirection).
2. What happens if you make a requests.get() call without specifying a timeout argument and the remote server never responds?
A. Python times out automatically after 30 seconds B. The script hangs indefinitely waiting for a response C. Python raises a ZeroDivisionError D. The request is rerouted to localhost Answer: B Explanation: By default, requests does not enforce a timeout; without setting timeout=..., code will block forever if the socket hangs.
3. Which method raises an HTTPError if the response status indicates an error (4xx or 5xx)?
A. response.check_error() B. response.raise_for_status() C. response.assert_ok() D. response.throw_if_failed() Answer: B Explanation: response.raise_for_status() checks the status code and raises a requests.exceptions.HTTPError if it represents a client or server error.
4. What is the difference between response.text and response.content?
A. response.text gives Unicode string text, while response.content gives raw binary bytes B. response.text gives HTTP headers, while response.content gives the body C. response.text is deprecated D. response.content is only for JSON files Answer: A Explanation: response.text decodes the payload into a Python string based on HTTP headers, whereas response.content provides raw un-decoded bytes (essential for images/PDFs).
5. What is the common base class for all exceptions raised by the requests library?
A. NetworkError B. IOError C. requests.exceptions.RequestException D. requests.BaseError Answer: C Explanation: All exceptions thrown by requests inherit from requests.exceptions.RequestException, enabling clean catch-all handlers.
GET & POST Methods
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Project: Infinite Sequence Generator | GET & POST Methods |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.