Kodeclik Blog
How to send a POST Request in Python
POST requests let your Python programs interact with web servers by sending data for creation, modification, or processing. They are essential for anything beyond simply reading information online, such as logging in, uploading files, or working with APIs.
What is a POST request?
A POST request is a way for your Python program to communicate with an external server by sending data for processing.
The name “POST” comes from the HTTP specification, which defines different methods computers use to talk to each other over the web. While a GET request simply retrieves information, a POST request actively sends data to the server for storage, processing, or further actions. For example, every time you log into a website, upload a file, or submit new information through a form, your browser or program is making a POST request behind the scenes.
When will you need to send a POST request?
You will need a POST request whenever your program has to send data that modifies or creates information on the server side. This could mean registering a new account, saving form submissions, or writing content into a remote database.
POST requests are also the go-to method when working with APIs that require input data. For example, you might send text to a machine learning API for sentiment analysis, instruct GitHub’s API to create a new repository, or post a comment on a social media feed using the platform’s developer interface. Without POST requests, applications would only be able to read data from the internet, not actually interact with it in a meaningful way.
Example 1: Login form on a website
A common example of a POST request is when you submit a login form on a website. The username and password you enter aren’t just magically checked by your local browser. Instead, the browser sends your credentials in a POST request to the website’s backend server, which then processes and validates them before granting access.
Example 2: Sending JSON data to an API endpoint
Another example involves sending JSON data to an API endpoint. Imagine building an application where you need to create a new user record or upload data to a database. Instead of manually entering that information into the server yourself, your Python program can issue a POST request with the data to the API. The server then processes it and saves the new record.
Example 3: Uploading a file
POST requests also come into play when you upload a file. If you’ve ever attached a document to a form submission or uploaded an image to a social network, you’ve essentially participated in a POST request where the file was packaged and sent to the server for storage.
Public URL for sending POST requests
If you want to try POST requests safely, there’s a convenient testing endpoint you can use: https://httpbin.org/post. This site simply echoes back whatever payload you send. That means you can experiment freely without worrying about breaking a real system or accidentally altering production data. It is an especially useful resource for beginners to verify that their code is correctly sending requests in the way they expect.
Sending POST requests using the Python requests library
Python’s requests library makes sending POST requests straightforward. Below is a basic example where we submit form-style data to a test URL.
import requests
# URL for testing
url = "https://httpbin.org/post"
# Data to send
payload = {
"username": "test_user",
"password": "secret123"
}
# Make the POST request
response = requests.post(url, data=payload)
# Print status code
print("Status Code:", response.status_code)
# Print response JSON
print("Response JSON:", response.json())
In this example, the data argument sends the information as if it were form data, similar to what you’d enter into a login input field on a website. The server responds with JSON, allowing you to verify what it received.
Sending POST requests using JSON instead of form data
Many modern APIs expect requests in JSON format rather than simple form fields. Fortunately, the requests library makes this easy. Instead of using the data argument, you can use json=payload.
import requests
url = "https://httpbin.org/post"
payload = {"message": "Hello, world!"}
response = requests.post(url, json=payload)
print(response.json())
Notice that when using json=payload, the library automatically sets the correct headers, including Content-Type: application/json. This ensures the server understands that the data is structured JSON and can parse it correctly.
Summary
POST requests are one of the most common ways to send data from Python to a server or API. Unlike GET requests that only fetch information, POST allows your code to create, update, and interact with servers in powerful ways. Whether you’re submitting login credentials, sending JSON data, or uploading files, mastering POST requests is an essential skill for any Python developer working with APIs. With tools like the requests library and testing endpoints such as httpbin, you can confidently experiment and build applications that communicate seamlessly with the web.
Enjoy this blogpost? Want to learn Python with us? Sign up for 1:1 or small group classes.