Python > Working with External Resources > Networking > HTTP Clients (`requests` library)
Basic HTTP GET Request with `requests`
This snippet demonstrates a simple HTTP GET request using the `requests` library to fetch data from a specified URL. It covers error handling for common HTTP status codes.
Code
This code sends an HTTP GET request to the URL 'https://jsonplaceholder.typicode.com/todos/1'. It then attempts to parse the response as JSON and prints the data and status code. Crucially, it uses a `try...except` block to handle potential errors such as HTTP errors (4xx or 5xx status codes), connection errors, timeout errors, and other request-related exceptions. `response.raise_for_status()` automatically checks if the HTTP status code indicates an error and raises an `HTTPError` if it does.
import requests
url = 'https://jsonplaceholder.typicode.com/todos/1'
try:
response = requests.get(url)
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
data = response.json()
print(f'Data: {data}')
print(f'Status Code: {response.status_code}')
except requests.exceptions.HTTPError as errh:
print(f'HTTP Error: {errh}')
except requests.exceptions.ConnectionError as errc:
print(f'Connection Error: {errc}')
except requests.exceptions.Timeout as errt:
print(f'Timeout Error: {errt}')
except requests.exceptions.RequestException as err:
print(f'Something went wrong: {err}')
Concepts Behind the Snippet
This snippet illustrates the fundamental concepts of making HTTP requests in Python. The `requests` library simplifies the process of sending HTTP requests and handling responses. Key concepts include:
Real-Life Use Case
Imagine you're building an application that needs to display weather data. You could use the `requests` library to fetch weather data from a public weather API, parse the JSON response, and display the relevant information (temperature, humidity, etc.) to the user. Similarly, you can retrieve product information from an e-commerce API or news articles from a news API.
Best Practices
Here are some best practices to keep in mind when working with the `requests` library:
Interview Tip
During interviews, be prepared to discuss the different HTTP methods (GET, POST, PUT, DELETE), the importance of error handling, and the benefits of using `requests.Session()` for efficient HTTP communication. You should also be able to explain how to handle different HTTP status codes.
When to Use Them
Use the `requests` library whenever you need to interact with external APIs or web services. It's suitable for tasks such as fetching data, submitting forms, and authenticating with servers. It's particularly useful for automating tasks that would otherwise require manual interaction with a web browser.
Memory Footprint
The memory footprint of `requests` is generally reasonable for most use cases. However, if you're dealing with extremely large responses (e.g., large files), you might consider streaming the response data instead of loading it all into memory at once. This can be achieved by setting `stream=True` in the `requests.get()` call.
Alternatives
While `requests` is the most popular HTTP client library in Python, other options exist:
Pros
The `requests` library offers several advantages:
Cons
Some potential drawbacks of `requests` include:
FAQ
-
What is the difference between `requests.get()` and `requests.post()`?
`requests.get()` is used to retrieve data from a server, while `requests.post()` is used to send data to a server (e.g., submitting a form). GET requests typically do not modify data on the server, while POST requests often do. -
How do I send data in a POST request?
You can send data in a POST request using the `data` or `json` parameters of the `requests.post()` function. The `data` parameter is used for sending data in the `application/x-www-form-urlencoded` format, while the `json` parameter is used for sending data in the `application/json` format. -
How do I set a timeout for a request?
You can set a timeout for a request using the `timeout` parameter of the `requests.get()` or `requests.post()` function. For example: `response = requests.get(url, timeout=5)` will set a timeout of 5 seconds.