Practical Guide to Using the Requests Library in Python

In Python, if you want to interact with the internet, the Requests library is definitely a “must-have”. Whether it’s scraping web data or interacting with API interfaces, Requests makes HTTP requests super simple. Today, we’ll break down this amazing tool’s usage and provide some practical examples, so by the end, you’ll be able to easily write efficient HTTP request code!

---

What is the Requests Library?

In simple terms, the Requests library is a module in Python used to send HTTP requests. It supports various HTTP methods, such as GET, POST, PUT, and DELETE. Compared to the native urllib module, Requests is more user-friendly and the code is simpler.

Don’t rush to read the documentation; let’s start with a small example to get a feel for it:

import requests

response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
print(response.json())

After running it, you’ll see a beautiful JSON data, which is the content retrieved from the API. Isn’t it simpler than opening a bottle of mineral water?

---

GET Request: Fetching Data from the Internet

GET is the most commonly used HTTP method for retrieving data from a server. For example, if you want to get a news article from an API, just send a GET request.

Example Code

import requests

url = 'https://jsonplaceholder.typicode.com/posts'
params = {'userId': 1}  # Attach query parameters
response = requests.get(url, params=params)

print(response.status_code)  # Status code
print(response.headers['Content-Type'])  # Response headers
print(response.json())  # Response content

Result: The returned content will be all articles for user ID 1.

Tip: The params parameter is used to concatenate the query string at the end of the URL. If you directly shove the parameters into the URL, it may cause errors.

---

POST Request: Submitting Data to the Server

POST is like sending a letter, where you “mail” your data to the server. For example, in scenarios like submitting forms or logging in, POST is frequently used.

Example Code

import requests

url = 'https://jsonplaceholder.typicode.com/posts'
data = {
    'title': 'Python Requests',
    'body': 'So useful! ',
    'userId': 1
}

response = requests.post(url, json=data)  # The json parameter directly converts the dictionary to JSON format
print(response.status_code)
print(response.json())

Using json=data here allows you to send the data to the server without the hassle of manually converting it to JSON format.

Common Issue: Many people use data=data instead of json=data. The difference is that the former sends application/x-www-form-urlencoded data, while the latter sends application/json directly. Pay attention to the documentation requirements when using APIs.

---

Handling Responses: What the Server Says

Sending requests is not hard, but it’s not enough to just send them; you need to check what feedback the server gives you. At this point, you need to study the contents of the Response object.

Main Attributes and Methods

import requests

response = requests.get('https://jsonplaceholder.typicode.com/posts/1')

print(response.status_code)  # Status code
print(response.text)  # Raw response text
print(response.json())  # JSON formatted response data
print(response.headers)  # Response headers

Learn to Interpret Status Codes:

200: Success (Well done!)
404: Resource not found (Lost your way)
500: Server error (Not your fault)

---

Custom Request Headers: Playing Different “Roles”

Sometimes you need to disguise yourself, for example, to mimic a browser sending a request. This requires custom request headers (headers).

Example Code

import requests

url = 'https://httpbin.org/headers'
headers = {
    'User-Agent': 'MyApp/1.0'
}

response = requests.get(url, headers=headers)
print(response.json())

By adding User-Agent, your request will appear more like it came from a specific application or browser.

---

Handling Timeouts: Don’t Get “Stuck” by the Server

In poor network conditions, you may encounter request timeouts. Requests provides a timeout parameter that allows you to set a maximum waiting time.

Example Code

import requests

try:
    response = requests.get('https://httpbin.org/delay/5', timeout=3)
    print(response.text)
except requests.exceptions.Timeout:
    print('Request timed out!')

Here, timeout=3 means it will wait a maximum of 3 seconds; if it exceeds that, it will throw an error.

Tip: Setting the timeout parameter is very important, especially in web scraping or bulk request scenarios, as it can prevent the program from “hanging”.

---

Uploading Files: Just Send It, No Hassle

The Requests library also handles file uploads smoothly. To upload a file, use the files parameter and pass the file object.

Example Code

import requests

url = 'https://httpbin.org/post'
files = {'file': open('example.txt', 'rb')}  # File mode must be 'rb'
response = requests.post(url, files=files)

print(response.json())

The above code will upload the example.txt file to the server. Isn’t that a lot of hassle saved?

---

Managing Cookies: Remember Your Identity

Some websites require cookies to maintain login states, and Requests supports managing them using the cookies parameter.

Example Code

import requests

# Sending a request with cookies
cookies = {'session_id': '12345'}
response = requests.get('https://httpbin.org/cookies', cookies=cookies)
print(response.json())

If you need to save and send cookies across multiple requests, you can use requests.Session().

---

Basic Authentication: A Step Towards Security

When accessing certain protected APIs, you need to provide a username and password, and the auth parameter in Requests makes this process easier.

Example Code

import requests
from requests.auth import HTTPBasicAuth

url = 'https://httpbin.org/basic-auth/user/pass'
response = requests.get(url, auth=HTTPBasicAuth('user', 'pass'))

print(response.status_code)
print(response.json())

Tip: Never hard-code real usernames and passwords in your code! It’s best to manage them with environment variables or configuration files.

---

Summary

The Requests library is like a Swiss Army knife; whether it’s GET, POST, file uploads, or handling authentication, everything is simple and straightforward. The next time you want to interact with the internet, give it a try, and you won’t be disappointed!

Leave a Comment