Sending POST Request with Python Requests Library

To send a POST request using the Python Requests Library, you should call the requests.post() method and pass the target URL as the first parameter and the POST data with the data= parameter. You must also specify the data type in the body of the POST message using the Content-Type request header so that the server can correctly receive and process the POST data. If no Content-Type header is passed to the requests.post() method, the application/x-www-form-urlencoded will be used. In this Python Requests POST example, we make a POST request to the ReqBin echo URL. Click Execute to run the Python Requests POST example online and see the result.
Sending POST Request with Python Requests Library Execute
import requests

url = 'https://reqbin.com/echo/post/xml'
xml = """<?xml version = "1.0" encoding = "UTF-8"?>
<Order>
  <Id>78912</Id>
  <Customer>Jason Sweet</Customer>
</Order>"""
headers = {'Content-Type': 'application/xml'}

r = requests.post(url, data=xml, headers=headers)

print(r.text)

Updated: Viewed: 17613 times

What is the Python Request Library?

Requests Library is a very popular library for sending HTTP requests in Python. The Requests Library 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.

What is HTTP POST?

The POST method requests the server to receive, process and store the data contained in the body of the POST message. The HTTP POST method is used to submit data to the server, upload files and images, and submit HTML forms. POST is one of the nine standard HTTP methods. Unlike HTTP GET and HTTP HEAD methods, HTTP POST request method can change server state and is not idempotent.

HTTP POST Request Example
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Accept: application/json
Content-Type: application/json
Content-Length: 81

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

How to use the 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 install the Python requests Library with 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 Python code:

import requests

For more information, visit the Python Requests Library.

Python Requests POST method syntax

Python Requests method syntax for making POST requests :

Python Requests POST Syntax
requests.post(URL, data=[data], json=[json], headers=[headers], cookies=[cookies], files=[files] arguments)

Where:
  • URL: target URL/API point.
  • data (optional): the data to send to the server, can be a dictionary, a list of tuples, bytes, or a file
  • json (optional): a dictionary that will be converted to a JSON string and included in the body of the POST request.
  • headers (optional): a list of HTTP headers to send to the server.
  • cookies (optional): a list of HTTP Cookies to send to the server.
  • files (optional): a list of files to upload to the server.
  • arguments (optional): other optional arguments that the POST request accepts.

Python POST Example using the Requests Library

Below is an example of posting an HTML form using the Python Requests library to the ReqBin echo URL:

POST HTML Form Example
import requests

url = "https://reqbin.com/echo/post/form"
data = "key1=value1&key2=value2"
headers = {'Content-Type': 'application/x-www-form-urlencoded'}

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

print(r.text)

In this example, the "import requests" directive imports the Requests library to our Python code. The Content-Type request header specifies the MIME data type in the body of the POST request. Headers and data are passed to the requests.post() method with the headers= and data= parameters.

How to POST JSON using the Python Requests Library?

To POST JSON data to the server using the Requests Library, you can pass the JSON to the requests.post() method using the json= parameter.

POST JSON Example
import requests

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

r = requests.post(url, json={"Id": 12345})

print(r.json())

The Requests Library will automatically add a "Content-Type: application/json header" to our POST request.

How to upload a file using the Python Requests Library?

To upload a file to the server using the Python requests library pass the file to the requests.post() method with the files= parameter.

Upload a file with Python Requests Library
import requests

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

r = requests.post(url, files={"file_name": file_data}})

print(r.status_code)

How to upload multiple files using the Python Requests Library?

To upload multiple files, you need to pass the list of files to the requests.post() method. The Requests library will upload files using multipart/form-data content type.

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")
files = {'file_name1': file_data1, 'file_name2': file_data2, 'file_name3': file_data3}

r = requests.post(url, files=files)              

print(r.status_code)

How to send additional headers with Python POST Request?

To send additional headers with Python POST request using the Requests Library, pass the custom HTTP headers to the requests.post() method with the headers= parameter.

POST Request with custom HTTP Headers Example
import requests

url = "https://reqbin.com/echo/post/form"
data = {"Id": 12345}
headers = {"Content-Type": "application/x-www-form-urlencoded"} 

r = requests.post(url, data=data, headers=headers)
print(r.text)

How to send cookies with Python POST Request?

To send HTTP Cookies to the server using the Requests Library, pass cookies to the requests.post() method with the cookies= parameter.

POST Requests with Сookies Example
import requests

url = 'https://reqbin.com/echo/post/json'
cookies = {"key": "value"}

r = requests.post(url, cookies=cookies)

print(r.text)

How to send authentication credentials with Python POST Request?

To send user authentication credentials with a POST request, pass credentials to the requests.post() method using the auth= parameter.

POST Requests with Authentication Credentials Example
import requests
from requests.auth import HTTPBasicAuth
      
url = 'https://reqbin.com/echo/post/json'
r = requests.post(url, auth=HTTPBasicAuth('login', 'password'))
      
print(r.text)

See Also