You can use the Session object

import requests
headers = {'User-Agent': 'Mozilla/5.0'}
payload = {'username':'niceusername','password':'123456'}

session = requests.Session()
session.post('https://admin.example.com/login.php',headers=headers,data=payload)
# the session instance holds the cookie. So use it to get/post later.
# e.g. session.get('https://example.com/profile')
Answer from atupal on Stack Overflow
๐ŸŒ
Medium
techkamar.medium.com โ€บ sending-form-data-post-using-python-requests-library-55850c7a93d5
Sending FORM data POST using Python Requests library - tech kamar - Medium
June 26, 2024 - import requests url = "http://localhost:8080/login" form_data = {"username":"johndoe", "password": "user@1234"} resp = requests.post(url, data=form_data)
Discussions

Python Requests library - post method with form data help
That looks like my issue; for the ... the data im looking for. But the session doesn't seem to send the post... Also I'm not sure how to check the send header, I have a header included with a user-agent: "Mosilla/5.0"... in there as an artifact form a pervious iteration of this program. Unsure if its necessary so I may remove and replace. ... If you did this without python, manually ... More on reddit.com
๐ŸŒ r/webscraping
5
1
December 17, 2021
How to send a "multipart/form-data" with requests in python? - Stack Overflow
How to send a multipart/form-data with requests in python? How to send a file, I understand, but how to send the form data by this method can not understand. ... your question is not really clear. What do you want to achieve? Do you wish to send "multipart/form-data" without a file upload in the form? ... Basically, if you specify a files parameter (a dictionary), then requests will send a multipart/form-data POST ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
GET & POST Flask-Form data with requests library
Sure its possible. But Im not sure whats the question? I dont know flask-form but typically when you submit a form it is a POST request to one of your routes. In that route function you can access the form data and do whatever you want with it. Maybe take a look at Flask-WTF, iga probably the better library for forms. Also has good documentation to get you started. :) More on reddit.com
๐ŸŒ r/flask
3
1
April 1, 2024
Python 101: How to submit a web form

They refer to another article that uses selenium at the end of this blog post, but it's worth making this explicit here (since I struggled with it for a while): mechanize and request are not good if you need to interact with javascript on the website. The best bet I've found there has been Selenium, although it's not nearly as elegant with the browser window popping up.

More on reddit.com
๐ŸŒ r/Python
5
21
June 8, 2012
๐ŸŒ
Requests
requests.readthedocs.io โ€บ en โ€บ latest โ€บ user โ€บ quickstart
Quickstart โ€” Requests 2.32.5 documentation - Read the Docs
To do this, simply pass a dictionary to the data argument. Your dictionary of data will automatically be form-encoded when the request is made: >>> payload = {'key1': 'value1', 'key2': 'value2'} >>> r = requests.post('https://httpbin.org/post', data=payload) >>> print(r.text) { ...
๐ŸŒ
Reddit
reddit.com โ€บ r/webscraping โ€บ python requests library - post method with form data help
r/webscraping on Reddit: Python Requests library - post method with form data help
December 17, 2021 - All I had to do was add "accept": "*/*" to the header and I did have to copy the entire form-data into a dictionary and pass it in with the request. ... No probs, well done! ... I built a Google Reviews scraper with advanced features in Python.
๐ŸŒ
ReqBin
reqbin.com โ€บ req โ€บ python โ€บ yjok4snr โ€บ post-html-form-example
Python | How to post HTML form to the Server?
To post HTML form data to the server in URL-encoded format using Python, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the Python POST message in key=value format.
Top answer
1 of 16
397

Basically, if you specify a files parameter (a dictionary), then requests will send a multipart/form-data POST instead of a application/x-www-form-urlencoded POST. You are not limited to using actual files in that dictionary, however:

>>> import requests
>>> response = requests.post('http://httpbin.org/post', files=dict(foo='bar'))
>>> response.status_code
200

and httpbin.org lets you know what headers you posted with; in response.json() we have:

>>> from pprint import pprint
>>> pprint(response.json()['headers'])
{'Accept': '*/*',
 'Accept-Encoding': 'gzip, deflate',
 'Connection': 'close',
 'Content-Length': '141',
 'Content-Type': 'multipart/form-data; '
                 'boundary=c7cbfdd911b4e720f1dd8f479c50bc7f',
 'Host': 'httpbin.org',
 'User-Agent': 'python-requests/2.21.0'}

And just to be explicit: you should not set the Content-Type header when you use the files parameter, leave this to requests because it needs to specify a (unique) boundary value in the header that matches the value used in the request body.

Better still, you can further control the filename, content type and additional headers for each part by using a tuple instead of a single string or bytes object. The tuple is expected to contain between 2 and 4 elements; the filename, the content, optionally a content type, and an optional dictionary of further headers.

I'd use the tuple form with None as the filename, so that the filename="..." parameter is dropped from the request for those parts:

>>> files = {'foo': 'bar'}
>>> print(requests.Request('POST', 'http://httpbin.org/post', files=files).prepare().body.decode('utf8'))
--bb3f05a247b43eede27a124ef8b968c5
Content-Disposition: form-data; name="foo"; filename="foo"

bar
--bb3f05a247b43eede27a124ef8b968c5--
>>> files = {'foo': (None, 'bar')}
>>> print(requests.Request('POST', 'http://httpbin.org/post', files=files).prepare().body.decode('utf8'))
--d5ca8c90a869c5ae31f70fa3ddb23c76
Content-Disposition: form-data; name="foo"

bar
--d5ca8c90a869c5ae31f70fa3ddb23c76--

files can also be a list of two-value tuples, if you need ordering and/or multiple fields with the same name:

requests.post(
    'http://requestb.in/xucj9exu',
    files=(
        ('foo', (None, 'bar')),
        ('foo', (None, 'baz')),
        ('spam', (None, 'eggs')),
    )
)

If you specify both files and data, then it depends on the value of data what will be used to create the POST body. If data is a string, only it willl be used; otherwise both data and files are used, with the elements in data listed first.

There is also the excellent requests-toolbelt project, which includes advanced Multipart support. It takes field definitions in the same format as the files parameter, but unlike requests, it defaults to not setting a filename parameter. In addition, it can stream the request from open file objects, where requests will first construct the request body in memory:

from requests_toolbelt.multipart.encoder import MultipartEncoder

mp_encoder = MultipartEncoder(
    fields={
        'foo': 'bar',
        # plain file object, no filename or mime type produces a
        # Content-Disposition header with just the part name
        'spam': ('spam.txt', open('spam.txt', 'rb'), 'text/plain'),
    }
)
r = requests.post(
    'http://httpbin.org/post',
    data=mp_encoder,  # The MultipartEncoder is posted as data, don't use files=...!
    # The MultipartEncoder provides the content-type header with the boundary:
    headers={'Content-Type': mp_encoder.content_type}
)

Fields follow the same conventions; use a tuple with between 2 and 4 elements to add a filename, part mime-type or extra headers. Unlike the files parameter, no attempt is made to find a default filename value if you don't use a tuple.

2 of 16
147

Requests has changed since some of the previous answers were written. Have a look at this Issue on Github for more details and this comment for an example.

In short, the files parameter takes a dictionary with the key being the name of the form field and the value being either a string or a 2, 3 or 4-length tuple, as described in the section POST a Multipart-Encoded File in the Requests quickstart:

>>> url = 'http://httpbin.org/post'
>>> files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}

In the above, the tuple is composed as follows:

(filename, data, content_type, headers)

If the value is just a string, the filename will be the same as the key, as in the following:

>>> files = {'obvius_session_id': '72c2b6f406cdabd578c5fd7598557c52'}

Content-Disposition: form-data; name="obvius_session_id"; filename="obvius_session_id"
Content-Type: application/octet-stream

72c2b6f406cdabd578c5fd7598557c52

If the value is a tuple and the first entry is None the filename property will not be included:

>>> files = {'obvius_session_id': (None, '72c2b6f406cdabd578c5fd7598557c52')}

Content-Disposition: form-data; name="obvius_session_id"
Content-Type: application/octet-stream

72c2b6f406cdabd578c5fd7598557c52
๐ŸŒ
ScrapeOps
scrapeops.io โ€บ home โ€บ python web scraping playbook โ€บ python requests how to submit forms
How To Submit Forms With Python Requests | ScrapeOps
July 8, 2024 - ... Whether you're automating form ... this is your go-to function for submitting forms. It allows you to send form data to a server using an HTTP POST request....
Find elsewhere
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ bytes โ€บ how-to-send-multipart-form-data-with-requests-in-python
How to Send "multipart/form-data" with Requests in Python
January 29, 2025 - Let's see how we can use it to send multipart/form-data. Before we start, make sure you have the Requests library installed. If not, you can add it to your Python environment using pip: ... Let's say we need to upload a profile picture to a server. Here's a simple script that does that: import requests url = "http://example.com/upload" file_path = "/path/to/your/file.jpg" with open(file_path, "rb") as file: files = {'file': file} response = requests.post(url, files=files) print(response.status_code)
๐ŸŒ
GitHub
gist.github.com โ€บ srafay โ€บ 19e0a13fe7e402f0a79715b1ed3f6560
Python Request for form-data ยท GitHub
Python Request for form-data. GitHub Gist: instantly share code, notes, and snippets.
๐ŸŒ
Franklingu
franklingu.github.io โ€บ programming โ€บ 2017 โ€บ 10 โ€บ 30 โ€บ post-multipart-form-data-using-requests
Post multipart form data using Python requests | Junchao's blog
October 30, 2017 - In [1]: data_req = Request('POST', 'https://franklingu.github.io/', data={'name ...: ': 'normal'}).prepare() In [2]: print(data_req.body) name=normal In [3]: normal_multipart_req = Request('POST', 'https://franklingu.github.io/', ...: files={'name': open('test.txt', 'r'), 'name2': 'content'}).pre ...: pare() In [4]: print(normal_multipart_req.body.decode('utf-8')) --cdf39af4e1bf449384b62fef701eda7b Content-Disposition: form-data; name="name"; filename="name" test --cdf39af4e1bf449384b62fef701eda7b Content-Disposition: form-data; name="name2"; filename="name2" content --cdf39af4e1bf449384b62fef
๐ŸŒ
ProxiesAPI
proxiesapi.com โ€บ articles โ€บ sending-form-data-with-python-requests
Sending Form Data with Python Requests | ProxiesAPI
Works with Python 2 and 3 ยท To ... is the default encoding used by HTML forms. To send this type of form data: Add your form data to a dictionary: Make a POST request and pass the data in the ยท...
๐ŸŒ
ScrapeOps
scrapeops.io โ€บ home โ€บ python web scraping playbook โ€บ python requests post requests
Python Requests - How to Send POST Requests | ScrapeOps
April 12, 2023 - In this guide, we walk through how to send POST requests with Python Requests. Including how to POST form data and JSON data.
๐ŸŒ
FastAPI
fastapi.tiangolo.com โ€บ tutorial โ€บ request-forms
Form Data - FastAPI
To use forms, first install python-multipart. Make sure you create a virtual environment, activate it, and then install it, for example: ... from typing import Annotated from fastapi import FastAPI, Form app = FastAPI() @app.post("/login/") async def login(username: Annotated[str, Form()], password: Annotated[str, Form()]): return {"username": username}
๐ŸŒ
Scrapfly
scrapfly.io โ€บ blog โ€บ posts โ€บ how-to-python-requests-post
Guide to Python requests POST method - Scrapfly Blog
November 5, 2024 - It allows for customizable and straightforward data sending by specifying the URL, headers, and the data itself. The most common data types include JSON, form data, or raw body data, all handled easily with python requests POST method.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-upload-files-using-python-requests-library
How to Upload Files Using Python Requests Library - GeeksforGeeks
July 23, 2025 - In this example, below Python code sends a POST request to "https://httpbin.org/post", including both form data ('key': 'value') and a file ('file.txt') uploaded via the `requests` library.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_requests_post.asp
Python Requests post Method
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST ยท Python HOME Python Intro Python Get Started Python Syntax ... Python Variables Variable Names Assign Multiple Values Output Variables Global Variables Variable Exercises Code Challenge Python Data Types ... Python Strings Slicing Strings Modify Strings Concatenate Strings Format Strings Escape Characters String Methods String Exercises Code Challenge Python Booleans
๐ŸŒ
Quora
quora.com โ€บ How-do-you-pass-form-data-in-Python-requests
How to pass form data in Python requests - Quora
In simple terms, we are using the name="" attribute from the <input> tags to define the php variables, to change this to a get method, change POST to GET (case sensitive) and to add new ones use the syntax, $var = $_POST['inputname']; ... It really ...
๐ŸŒ
Oxylabs
oxylabs.io โ€บ blog โ€บ how-to-send-post-python-request
How to Send a POST with Python Requests?
To send a POST request using the Python requests library, you need to use the requests.post() method. This method allows you to send data to a server or API and is useful for tasks such as submitting form data or uploading files.
๐ŸŒ
ScrapingBee
scrapingbee.com โ€บ blog โ€บ how-to-send-post-python-requests
How to send a POST with Python Requests? | ScrapingBee
January 11, 2026 - To send a POST request in Python, you typically use requests.post. The main difference is what kind of data you're sending: Form data โ†’ use data=.... This sends application/x-www-form-urlencoded (similar to a regular web form).
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ urllib3 โ€บ python-urllib3-exercise-19.php
Python File Upload: Simulate POST request with multipart/Form-Data
import urllib3 # Create a PoolManager http = urllib3.PoolManager() # Specify the file and URL file_path = 'test.txt' upload_url = 'https://example.com/upload' # Prepare the multipart/form-data payload fields = { 'field1': 'value1', # Additional form fields if needed 'field2': 'value2', 'file': ('filename.txt', open(file_path, 'rb').read(), 'text/plain') } # Set the Content-Type header to multipart/form-data headers = {'Content-Type': 'multipart/form-data'} # Make the POST request response = http.request('POST', upload_url, fields=fields, headers=headers) # Receive and print the response print(response.data.decode('utf-8'))