How to make a POST request in Python

1 min readAPIRequests
7-Day Challenge

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.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

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
for Python 3.x
pip install requests
for Python 2.7

Performing the POST request

Once again we can use the test API endpoint : "https://reqbin.com/echo/post/json" that should return

{"success":"true"}
This is what we should get from this call
# 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 script to make an HTTP GET request against a website

The JSON dict response is now accessible from response.json()

print(response.json())
Printing the response JSON

Here you are! You can now make POST requests to any website using the requests library.

7-Day Challenge

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.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

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. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.