2022-08-10

requests

requests Module

The requests library is one of the most popular and widely-used libraries in Python for making HTTP requests. It abstracts the complexities of making requests behind a beautiful, simple API, allowing you to send HTTP/1.1 requests seamlessly. With it, you can add content like headers, form data, multipart files, and parameters via simple Python libraries to HTTP requests.

Installing requests Module

To install requests, run the following pip command:

bash
$ pip instal requests

GET Requests

A GET request is one of the most common HTTP methods. It is used to retrieve or read data from a resource. In Python's requests library, making a GET request is quite straightforward:

python
import requests

response = requests.get('https://api.github.com')

# Now we have a Response object called 'response'.
print(response.status_code)
200

POST Requests

A POST request is used to send data to a server to create a new resource. The data sent to the server with POST is stored in the request body. Here's an example of a POST request using requests:

python
import requests

data = {
    'name': 'John',
    'age': '30'
}

response = requests.post('https://httpbin.org/post', data=data)

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

In this example, we're sending a dictionary as data, which requests will form-encode before sending.

PUT Requests

A PUT request is similar to a POST request in that it sends data to the server. The difference is that PUT requests are idempotent, which means that making the same PUT request to a URL multiple times will always have the same result. Here's an example of a PUT request:

python
import requests

data = {
    'name': 'John',
    'age': '30'
}

response = requests.put('https://httpbin.org/put', data=data)

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

DELETE Requests

A DELETE request is used to delete a resource from the server. Here's an example of a DELETE request:

python
import requests

response = requests.delete('https://httpbin.org/delete')

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

HEAD Requests

A HEAD request is similar to a GET request, but it asks the server to return the response headers only, and not the actual resource (i.e., no message body). This is useful to check if a resource is available, or to check the response headers, without actually downloading the content. Here's an example of a HEAD request:

python
import requests

response = requests.head('https://httpbin.org/get')

print(response.status_code)
print(response.headers)

PATCH Requests

A PATCH request is used to apply partial modifications to a resource, rather than replacing the entire resource like PUT requests. Here's an example of a PATCH request:

python
import requests

data = {
    'name': 'John'
}

response = requests.patch('https://httpbin.org/patch', data=data)

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

Timeouts

By default, requests do not time out unless you set a timeout duration. It's generally a good practice to set timeout durations so that the request doesn't hang indefinitely. Here's how to set a timeout:

python
import requests

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

In this example, the request will raise a requests.exceptions.Timeout exception if it hasn't completed in 3 seconds.

Redirects

By default, GET, OPTIONS, POST, PUT, PATCH, and DELETE requests to a URL are automatically redirected to a new location (if the response status code is 301, 302, 303, 307, or 308). If you don't want this behavior, you can disable automatic redirection:

python
import requests

response = requests.get('http://github.com', allow_redirects=False)
print(response.status_code)

In this example, the GET request to http://github.com will return a 301 status code, but will not automatically follow the redirect to https://github.com because we have set allow_redirects=False.

Headers

Headers provide additional information about the request or the response. To add headers to your request, pass a dict to the headers parameter. Here's how to do it:

python
import requests

headers = {'user-agent': 'my-app/0.0.1'}

response = requests.get('https://httpbin.org/get', headers=headers)

print(response.json())

Sending JSON Data

JSON (JavaScript Object Notation) is a common data format with diverse uses in data interchange, including that of web services. Many APIs work with JSON data.

Here's an example of how you can send JSON data:

python
import requests
import json

data = {
    'name': 'John',
    'age': 30
}

headers = {'Content-type': 'application/json'}

response = requests.post('https://httpbin.org/post', data=json.dumps(data), headers=headers)

print(response.json())

In this example, we first import the json module to convert our Python dictionary to a JSON string using json.dumps(). We also set the Content-type header to application/json to inform the server that we're sending JSON data.

Sending Form Data

Form data is typically used to send data from an HTML form to a server. The data is sent as a series of key-value pairs, with each pair representing a form field and its value. The requests module makes it easy to send form data without needing to manually format it.

There are two types of form data that you can send: application/x-www-form-urlencoded and multipart/form-data. The former is the default and is suitable for most cases, but if you need to send files, you must use 'multipart/form-data'.

Here's an example of sending multipart/form-data:

python
import requests

data = {
    'name': 'John',
    'age': 30
}

files = {
    'file': open('file.txt', 'rb')
}

response = requests.post('https://httpbin.org/post', data=data, files=files)

print(response.json())

In this example, we open a file in binary mode, and include it in the files dictionary. When we make the POST request, requests will automatically set the Content-Type header to multipart/form-data, and format the data and file as necessary.

Remember to always close files after using them to free up system resources. A good way to ensure files are properly closed is by using the with statement:

python
with open('file.txt', 'rb') as f:
    files = {'file': f}
    response = requests.post('https://httpbin.org/post', data=data, files=files)

References

https://requests.readthedocs.io/en/latest/

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!