Introduction
As developers, we often work with APIs or web services, and one common task is sending data in JSON (JavaScript Object Notation) format to a server. Fortunately, Python provides us with a powerful requests
library, which makes HTTP requests a breeze, including sending JSON data.
In this article, we'll go through how to use the
requests
library to send a POST request with JSON data in Python. We'll cover everything from the basics of sending a POST request to handling the response received from the server, and even handling errors that may occur along the way.
Sending JSON Data via POST Request
Now, we can jump into the meat of this article - making a POST request with the requests
library in Python. Let's start with a simple example that sends JSON data in the body of the request. We first need to import the requests
library, specify the URL that we want to send the request to and define the JSON data in the data
variable. We also specify the Content-type
header to indicate that we are sending JSON data:
import requests
# Specify the API URL we want to send our JSON to
url = 'https://jsonplaceholder.typicode.com/posts'
# Specify the appropriate header for the POST request
headers = {'Content-type': 'application/json'}
# Specify the JSON data we want to send
data = '{"title": "foo", "body": "bar", "userId": 1}'
Then, we make the POST request using the requests.post()
method, passing in the URL, headers, and data:
response = requests.post(url, headers=headers, data=data)
Finally, we print the status code and response text to verify that the request was successful and that we received the expected response:
print(response.status_code)
print(response.text)
Note: We'll take a closer look at the responses we can get from a server in the following sections.
Now that we've seen a basic example, let's take a closer look at the parameters we passed to the requests.post()
method:
url
: The URL that we want to send the request to.headers
: A dictionary of headers to include in the request. In this case, we specify theContent-type
header to indicate that we are sending JSON data.data
: The data to send in the body of the request. This can be either a string or a dictionary. In our example, we passed a JSON string.
It's worth noting that there's another way to send JSON data in a POST request, using the json
parameter instead of data
:
import requests
url = 'https://jsonplaceholder.typicode.com/posts'
data = {'title': 'foo', 'body': 'bar', 'userId': 1}
# Pay attention here!
# We are using `json` parameter
# Instead of the `data` parameter
response = requests.post(url, json=data)
print(response.status_code)
print(response.text)
The json
parameter automatically sets the Content-type
header to application/json
and encodes the data as JSON in the request body.
Note: Both methods work equally well, so choose the one that fits your use case best.
And that's pretty much it for making a POST request with JSON data using the requests
library in Python. Now, we can take a look at how to handle the response received from the server.
Handling the Response
Now that we've learned how to make a POST request with JSON data, it's time to handle the response from the server. The response can give us important information about the success of the request and any data that was returned.
Let's expand the example from the previous section to make it handle the response appropriately. We'll first make the POST request using the same code as in the previous section. Then, we check the status code of the response using the response.status_code
attribute:
import requests
# 1. Setting parameters for the POST request
url = 'https://jsonplaceholder.typicode.com/posts'
headers = {'Content-type': 'application/json'}
data = '{"title": "foo", "body": "bar", "userId": 1}'
# 2. Sending the POST request
response = requests.post(url, headers=headers, data=data)
# 3. Handling the response from the server
if response.status_code == requests.codes.ok:
print('Request was successful')
print(response.json())
else:
print('Request failed with status code:', response.status_code)
If the status code is requests.codes.ok
, which is equivalent to 200
, we print a success message and the JSON data returned by the server using the response.json()
method. If the status code is anything other than 200
, we print an error message and the status code.
Note: The response.json()
method returns the JSON data from the response as a Python dictionary. This makes it easy to work with the data in your Python code.
Handling the response can also include checking for specific HTTP status codes or headers, parsing the response text, or working with the response content. The
requests
library provides a variety of methods and attributes to make it easy to handle the response from a request.
Handling Errors
When working with HTTP requests, it's important to handle errors that may occur during the process. In this section, we'll cover how to handle common errors you may face when making POST requests we've been discussing so far. Let's start with a simple example that demonstrates how to check for errors in the response:
import requests
url = 'https://jsonplaceholder.typicode.com/posts'
headers = {'Content-type': 'application/json'}
data = '{"title": "foo", "body": "bar", "userId": 1}'
response = requests.post(url, headers=headers, data=data)
if response.status_code != requests.codes.ok:
raise Exception('Request failed with status code:', response.status_code)
We made the POST request using the same code as in the previous sections. Then, we checked the status code of the response using the response.status_code
attribute.
If the status code is not 200
, we raise an exception with an error message and the status code. This ensures that the program stops executing if an error occurs, rather than continuing with potentially invalid data.
Other common errors that may occur when making a POST request with JSON data include:
ConnectionError
: raised when a connection to the server cannot be established.Timeout
: raised when the request times out.JSONDecodeError
: raised when the response cannot be decoded as JSON.
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
Let's take a look at an example that demonstrates how to handle all of these errors:
import requests
url = 'https://jsonplaceholder.typicode.com/posts'
headers = {'Content-type': 'application/json'}
data = '{"title": "foo", "body": "bar", "userId": 1}'
try:
response = requests.post(url, headers=headers, data=data, timeout=5)
response.raise_for_status()
json_data = response.json()
# Handle ConnectionError
except requests.exceptions.ConnectionError as ce:
print('Connection error:', ce)
# Handle Timeout
except requests.exceptions.Timeout as te:
print('Request timed out:', te)
# Handle HTTPError
except requests.exceptions.HTTPError as he:
print('HTTP error occurred:', he)
# Handle ValueError
except ValueError as ve:
print('JSON decoding error:', ve)
else:
print('Request was successful')
print(json_data)
Note: Here, we wrapped the POST request in a try-except
block in order to catch different types of exceptions that may occur.
The response.raise_for_status()
method is used to raise an exception if the status code indicates an error (e.g. 404
). If the request is successful, we print a success message and the JSON data returned by the server using the response.json()
method.
By handling errors that may occur when making a POST request with JSON data, we can write robust and reliable code that handles various scenarios.
Conclusion
In this article, we took a look at how to use the requests
library to make a POST request with JSON data in Python. We covered the basics of making a POST request, handling the response received from the server, and handling errors that may occur during the process.
We saw that the requests
library provides a simple and intuitive API for making HTTP requests, and that sending JSON data in a POST request is straightforward using either the data
or json
parameter. We also learned how to handle the response from the server, including checking the status code and extracting the JSON data returned.
Finally, we covered error handling and demonstrated how to handle common errors that may occur when making a POST request with JSON data.