python requests form data post

Python Requests Form Data Post

Python Requests is a popular HTTP library that enables developers to send HTTP requests using Python code. It provides methods for sending HTTP requests, handling responses, and working with cookies, authentication, and more.

When it comes to sending form data using POST request, Python Requests makes it extremely easy. The library allows developers to specify the form data as a dictionary or a list of tuples, and then sends it as the request body.

Here is an example of how to send form data using Python Requests:

import requests

url = ''
form_data = {'username': 'myusername', 'password': 'mypassword'}
response =, data=form_data)


In this example, we are sending a POST request to the URL '' with the form data included in the 'data' parameter. The response content is then printed to the console.

Multiple Ways to Specify Form Data

There are multiple ways to specify form data using Python Requests.

1. Dictionary

The most common way is to use a dictionary to specify the form data. Each key in the dictionary represents a form field name, and the corresponding value is the value of that field.

form_data = {'username': 'myusername', 'password': 'mypassword'}
response =, data=form_data)

2. List of Tuples

Another way to specify form data is by using a list of tuples. Each tuple contains two elements: the form field name and the corresponding value.

form_data = [('username', 'myusername'), ('password', 'mypassword')]
response =, data=form_data)

3. Custom Encoding

If you need to use a custom encoding for your form data, you can pass in the data as a string and specify the encoding using the 'headers' parameter.

form_data = 'username=myusername&password=mypassword'
headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'}
response =, data=form_data, headers=headers)

In this example, we are using the 'application/x-www-form-urlencoded' content type and specifying the encoding as 'utf-8'.


Python Requests makes it easy to send form data using a POST request. Whether you prefer to use a dictionary or a list of tuples, Python Requests can handle it. With its simple API and powerful features, Python Requests is a great choice for any HTTP-related task.