4.1 Handling Responses
The requests
module provides handy methods for
working with server responses.
Status Codes
Besides the response, the server also sends a request processing status.
Status info is available in the
status_code
and reason
fields. Check out the example below:
import requests
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
print(response.status_code) # Prints the status code of the response
print(response.reason) # Prints the textual description of the status
print(response.ok) # Returns True if the status code is less than 400
Headers
What's an HTTP request without headers, right? If you need the request
headers or response headers, you can access them through the headers
field:
import requests
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
print(response.headers) # Prints the response headers
print(response.headers['Content-Type']) # Prints the value of a specific header
Response Body
The server response can contain bytes, text, json
, or xml
. If
you know what exactly you're requesting from the server, you can directly
get an object of the desired type using one of
the methods/fields:
import requests
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
print(response.text) # Prints the response body as text
print(response.json()) # Prints the response body as JSON
print(response.content) # Prints the response body as bytes
More details about headers and response statuses can be learned in networking lectures.
4.2 Error Handling
The requests
module provides exceptions to
handle errors.
The HTTP standard doesn't imply exceptions; instead, it uses
error codes (status_code)
. If you want a Python exception to be thrown in case of
a failed request, you need to explicitly call the function
raise_for_status()
.
Example:
import requests
try:
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
response.raise_for_status() # Raises exceptions for 4xx and 5xx status codes
except requests.exceptions.HTTPError as err:
print(f"HTTP error occurred: {err}")
except Exception as err:
print(f"Other error occurred: {err}")
else:
print("Success!")
More details about error codes can be learned in networking lectures.
4.3 Sending Data
Before JSON was invented, large amounts of data were sent through
"forms". A form is a special browser page object (and
a data standard in HTTP). If you want to send data
"via form", you just need to pass the
data
parameter along with the request.
Important!
GET requests don't support
forms as they don't have a request body. All their data is passed
only in the URL.
Sending Data in a GET Request
In a GET request
, data is passed through URL parameters. Here's an example:
import requests
params = {'key1': 'value1', 'key2': 'value2'}
response = requests.get('https://httpbin.org/get', params=params)
print(response.url) # Prints the URL with added parameters
Sending Form Data
We're going to use a POST request
to send data to the server.
Example:
import requests
data = {
'username': 'example',
'password': 'password'
}
response = requests.post('https://httpbin.org/post', data=data)
print(response.json())
Sending Files
If you want to send binary data over the internet, like
uploading a picture, you need to pass your file or files
in the request using the files
parameter.
Example:
import requests
files = {'file': open('example.txt', 'rb')}
response = requests.post('https://httpbin.org/post', files=files)
print(response.json())
Easy as pie. If you want to send multiple files, just list
them under any names in the files
variable.
Important!
Don't forget to close the file after sending to avoid resource leaks.
It's best to use a with
statement, which automatically closes the file after the operation:
import requests
with open('example.txt', 'rb') as f:
files = {'file': f}
response = requests.post('https://httpbin.org/post', files=files)
print(response.json())
4.4 Login and Authorization
API (Application Programming Interface) is a set of rules and protocols that allow different programs to interact with each other. Many websites and services only allow requests and API interactions after logging in.
After a successful login, you get a special object—a session (session)
, which contains a unique number
of your "authorized session with the server". For further requests, you'll need to use this object.
Authentication
To log in to the server, you need to go through authentication (login process), for which you need to pass credentials with the request.
import requests
from requests.auth import HTTPBasicAuth
response = requests.get('https://httpbin.org/basic-auth/user/pass', \
auth=HTTPBasicAuth('user', 'pass'))
print(response.status_code)
This is how authorization looks, but it's usually used with a session.
Using Sessions
Sessions allow retaining parameters between requests, such as cookies
or user authorization details.
import requests
payload = {
'username': 'your_username',
'password': 'your_password'
}
# Creating a session
session = requests.Session()
# Logging into the site
login_response = session.post('https://example.com/login', data = payload)
# Further actions as a logged-in user
data_response = session.get('https://example.com/api/data')
print(data_response.json())
GO TO FULL VERSION