How do I send a POST request with a Python Requests Library?

Python Requests is a very popular library for sending HTTP requests in python. It greatly simplifies the process of working with HTTP requests in python. The Requests library is very flexible and can send any type of HTTP request using POST, GET and DELETE methods, upload files, post JSON and XML, and submit HTML forms. The code written using the Python Requests library is simple and easy to read. Although Python has a built-in urllib3 module for handling HTTP requests with similar functionality, almost everyone uses the Requests library because of its simplicity and convenience.

An example of posting an HTML form using the Python Requests library.

Post HTML Form with Python Requests Library
import requests
from requests.structures import CaseInsensitiveDict

url = "https://reqbin.com/echo/post/form"

headers = CaseInsensitiveDict()
headers["Content-Type"] = "application/x-www-form-urlencoded"

data = "key1=value1&key2=value2"

resp = requests.post(url, headers=headers, data=data)

print(resp.status_code)

In this post HTML form example, the import requests directive imports the Requests library to our python code. The headers["Content-Type"] = "application/x-www-form-urlencoded" line sets the MIME type to application/x-www-form-urlencoded for the data in the body of POST message and requests.post(url, headers=headers, data=data) line sends data to the server.

Posting JSON data with the Requests library is also very easy. To do this, you need to set Content-Type to application/json and pass JSON to requests.post() method.

Post JSON with Python Requests Library
import requests
from requests.structures import CaseInsensitiveDict

url = "https://reqbin.com/echo/post/json"

headers = CaseInsensitiveDict()
headers["Accept"] = "application/json"
headers["Content-Type"] = "application/json"

data = """
{
  "Id": 78912,
  "Customer": "Jason Sweet",
  "Quantity": 1,
  "Price": 18.00
}
"""

resp = requests.post(url, headers=headers, data=data)

print(resp.status_code)

In this post JSON example, we also send the Accept: application/json request header to tell the server that our client is expecting JSON. Without this header, the server may return data in a different format.

Uploading files using Python Requests Library is also very easy.

Upload a file with Python Requests Library
import requests

url = "https://reqbin.com/echo/post"
file_data = open("file_name.txt", "rb")

resp = requests.post(url, files = {"form_name": file_data}})

print(resp.status_code)

To upload multiple files, simply pass the list of files to the requests.post() method.

Uploading multiple files with Python Requests Library
import requests

url = "https://reqbin.com/echo/post"
file_data1 = open("file_name1.txt", "rb")
file_data2 = open("file_name2.txt", "rb")
file_data3 = open("file_name3.txt", "rb")

resp = requests.post(url, files = [("form_name1", file_data1),
              ("form_name2", file_data2),
              ("form_name3", file_data3)]})

print(resp.status_code)

Getting Started with Python Requests Library

To use the Python Requests Library, you need to install it first. You can do this with the following command:

$ pip install requests

If you prefer using Pipenv, you can run the following command:

$ pipenv install requests

After installing the Requests library, you can use it in your code by importing the requests module with the following code:

import requests

For more information, visit the Python Requests Library home page.
Last updated: Viewed: 104 times