How to make a POST request in Python
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
As for the GET request, we can use the requests library to perform a POST request in Python.
POST requests are extremely useful when you want to get data from APIs.
But even more when you want to send data.
To APIs or Apps so that they can process it and save it in their database. (e.g. Hubspot adding emails).
Installing requests
First we need to install the requests library using the pip python package manager
You can write this in your terminal, it will automatically install the requests library.
pip3 install requests
pip install requests
Performing the POST request
Once again we can use the test API endpoint : "https://reqbin.com/echo/post/json" that should return
{"success":"true"}
# We import the library
import requests
# We set the api endpoint we want to reach
url = "https://reqbin.com/echo/post/json"
# We define the data we want to pass
data = {
"firstname" : "James",
"lastname" : "Smith",
"age": 27
}
# We perform the post request against the api endpoint
# we pass the dictionary data as body.
response = requests.post(url, data=data)
print(response.json())
The JSON dict response is now accessible from response.json()
print(response.json())
Here you are! You can now make POST requests to any website using the requests library.
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚