3.1 Module requests
For basic internet operations in Python, there's a library called requests. It provides an easy interface for network tasks, allowing you to send HTTP requests and get responses with minimal effort.
This library doesn't come with Python by default, so before using it you need to install it via a package manager.
Open your terminal and type:
pip install requests
After installing the library, you can use the requests module in your projects. For instance, here's how you can find out your external IP address:
import requests
response = requests.get("http://ip.jsontest.com/")
print(response.status_code) # Prints the response status code
print(response.json()) # Prints JSON response
Here we sent a request to the service (site) ip.jsontest.com, which returns a JSON object with the IP address from which the request was made. The request and response retrieval is done in a single line — it's super convenient. In the other lines, we simply printed the server response.
In the example above, we called the get() method, and below I'll tell you about this method and other methods of the requests object.
3.2 List of Methods
The requests object has methods for every occasion, or rather, one method for each type of HTTP request.
| Method | Description |
|---|---|
requests.get() |
Sends a GET request to retrieve data from a server. |
requests.post() |
Sends a POST request to send data to a server. |
requests.put() |
Sends a PUT request to update data on a server. |
requests.delete() |
Sends a DELETE request to remove data from a server. |
requests.head() |
Sends a HEAD request to retrieve headers without the response body. |
requests.options() |
Sends an OPTIONS request to get supported methods and options from a server. |
requests.patch() |
Sends a PATCH request to partially update data on a server. |
requests.request() |
Main method for sending all types of HTTP requests. |
Below, we'll look at the most essential ones.
3.3 Sending a GET request and POST request
A GET request usually consists only of a URL, or it might include an additional query string.
1. Sending a plain GET request
A GET request is used to retrieve data from a server. The required parameter is the URL to which the request is sent.
Example:
import requests
response = requests.get('https://jsonplaceholder.typicode.com/posts')
print(response.status_code) # Prints the response status code
print(response.json()) # Prints JSON response
2. Sending a GET request with parameters
Also, a URL can have additional parameters in the form of a string key=value&key2=value2&…
In our case, parameters are passed as a dictionary and the variable params:
import requests
params = {'userId': 1}
response = requests.get('https://jsonplaceholder.typicode.com/posts', params=params)
print(response.json())
3. Sending a POST request
A POST request can contain a request body: text, JSON, or even an image.
Example:
import requests
data = {
'title': 'foo',
'body': 'bar',
'userId': 1
}
response = requests.post('https://jsonplaceholder.typicode.com/posts', json=data)
print(response.status_code)
print(response.json())
3.4 Sending a PUT request and DELETE request
1. Sending a PUT request
A PUT request is used to update data on a server.
import requests
data = {
'title': 'foo',
'body': 'bar',
'userId': 1
}
response = requests.put('https://jsonplaceholder.typicode.com/posts/1', json=data)
print(response.status_code)
print(response.json())
2. Sending a DELETE request
A DELETE request is used to remove data from a server.
import requests
response = requests.delete('https://jsonplaceholder.typicode.com/posts/1')
print(response.status_code)
print(response.json())
To better understand the essence of GET, POST, PUT, and DELETE requests, you need to read lectures about how networks work, the internet, web, and HTTP. All this awaits you in the near future.
GO TO FULL VERSION