How to Upload Files Using Python Requests Library
Last Updated :
27 Mar, 2024
We are given some files and our task is to upload it using request library of Python. In this article, we're going to discover a way to use the requests library to add files in diverse scenarios, such as uploading unmarried documents, multiple files, and documents with extra form statistics
Upload Files Using Python Requests Library
Below, are the examples of Python Upload Files With Python's Requests Library. Before diving into file upload examples, let's ensure you have the requests library installed. If not, you can install it using pip:
pip install requests
Below, are the methods of uploading files with Python's Requests library
- Using files parameter
- Using data parameter
- Using multipart/form-data directly
- Sending as raw binary data
File Structure

file.txt
GeeksforGeeks
Upload Files Using Files Parameter
In this example, below code uses the Python requests library to upload a file (file.txt) to the specified URL (https://httpbin.org/post) using a POST request with the files parameter, and then prints the response text.
Python3
import requests
url = 'https://httpbin.org/post'
files = {'file': open('file.txt', 'rb')} # Specify the file you want to upload
response = requests.post(url, files=files)
print(response.text)
Output
{
"args": {},
"data": "",
"files": {
"file": "GeeksforGeeks"
},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "157",
"Content-Type": "multipart/form-data; boundary=c4e3e1f85303491ffd523537762f87bc",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.31.0",
"X-Amzn-Trace-Id": "Root=1-65fbfabc-4d943f6b5851070275fbb946"
},
"json": null,
"origin": "157.34.0.92",
"url": "https://httpbin.org/post"
}
Upload Files Using Data Parameter
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. It then prints the response received from the server.
Python3
import requests
url = 'https://httpbin.org/post'
data = {'key': 'value'} # Additional data if required
with open('file.txt', 'rb') as file:
response = requests.post(url, data=data, files={'file': file})
print(response.text)
Output
{
"args": {},
"data": "",
"files": {
"file": "GeeksforGeeks"
},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "157",
"Content-Type": "multipart/form-data; boundary=c4e3e1f85303491ffd523537762f87bc",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.31.0",
"X-Amzn-Trace-Id": "Root=1-65fbfabc-4d943f6b5851070275fbb946"
},
"json": null,
"origin": "157.34.0.92",
"url": "https://httpbin.org/post"
}
Upload Files Using Multipart/Form-data Directly
In this example, below Python code uses the requests library to perform a POST request to https://httpbin.org/post, uploading the file example.txt with the specified content type of multipart/form-data. The server's response is then printed.
Python3
import requests
url = 'https://httpbin.org/post'
files = {'file': ('example.txt', open('example.txt', 'rb'),
'multipart/form-data')}
response = requests.post(url, files=files)
print(response.text)
Output
{
"args": {},
"data": "",
"files": {
"file": "GeeksforGeeks"
},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "157",
"Content-Type": "multipart/form-data; boundary=c4e3e1f85303491ffd523537762f87bc",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.31.0",
"X-Amzn-Trace-Id": "Root=1-65fbfabc-4d943f6b5851070275fbb946"
},
"json": null,
"origin": "157.34.0.92",
"url": "https://httpbin.org/post"
}
Sending as Raw Binary Data
In this example, below code sends a POST request to 'https://httpbin.org/post' with a header specifying the content type as 'application/octet-stream'. It uploads the contents of the file 'file.txt' as raw binary data. The server's response is then printed.
Python3
import requests
url = 'https://httpbin.org/post'
# Specify the content type
headers = {'Content-Type': 'application/octet-stream'}
data = open('file.txt', 'rb').read()
response = requests.post(url, headers=headers, data=data)
print(response.text)
Output
{
"args": {},
"data": "",
"files": {
"file": "GeeksforGeeks"
},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "157",
"Content-Type": "multipart/form-data; boundary=c4e3e1f85303491ffd523537762f87bc",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.31.0",
"X-Amzn-Trace-Id": "Root=1-65fbfabc-4d943f6b5851070275fbb946"
},
"json": null,
"origin": "157.34.0.92",
"url": "https://httpbin.org/post"
}
Similar Reads
How to Download and Upload Files in FTP Server using Python?
Prerequisite: FTP, ftplib Here, we will learn how to Download and Upload Files in FTP Server Using Python. Before we get started, first we will understand what is FTP. FTP(File Transfer Protocol) File Transfer Protocol(FTP) is an application layer protocol that moves files between local and remote f
3 min read
How to test file uploads in Postman using Express?
In web development, handling file uploads is a major requirement for various APIs. Postman is a powerful tool used for API testing, facilitates the process of testing file uploads. In this article we will see how we can test file upload with the help of Postman. Prerequisites:Node and NPM/Yarn insta
3 min read
How to Upload File in Python-Flask
File uploading is a typical task in web apps. Taking care of file upload in Flask is simple all we need is to have an HTML form with the encryption set to multipart/form information to publish the file into the URL. The server-side flask script brings the file from the request object utilizing the r
2 min read
How To Download Large File In Python With Requests
Downloading large files in Python can sometimes be a tricky task, especially when efficiency and reliability are crucial. Python's requests library provides a convenient way to handle HTTP requests, including downloading files. In this article, we will explore how to download large files in Python w
2 min read
How to upload files using HTML to website ?
Every file that needs to be uploaded to the website, required the basic form which facilitates uploading. This feature is essential when we are filling out the form on a specific website. This file upload may support a variety of file formats along with various types of files. The file uploading fea
2 min read
How to Upload JSON File to Amazon DynamoDB using Python?
Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. It offers scalability, reliability, and fast access to data for applications of any scale. DynamoDB supports both document and key-value data models and handles adminis
3 min read
How To Upload And Download Files From AWS S3 Using Python?
Pre-requisite: AWS and S3Amazon Web Services (AWS) offers on-demand cloud services which means it only charges on the services we use (pay-as-you-go pricing). AWS S3 is a cloud storage service from AWS. S3 stands for 'Simple Storage Service. It is scalable, cost-effective, simple, and secure. We gen
3 min read
How to Upload Files in Ruby on Rails?
Uploading files in a web application is a common requirement, whether it's for user profile pictures, documents, or any other files. Ruby on Rails makes this task straightforward with its built-in tools. In this article, we'll walk through the steps to set up file uploads in a Rails app, from creati
6 min read
How To Export Specific Request To File Using Postman?
Postman is a popular tool used for API testing and development. It provides a user-friendly interface to make HTTP requests, track responses, and organize API endpoints. One of the powerful features of Postman is the ability to export specific requests to a file. This is useful for sharing requests
3 min read
How to Fix Python Requests SSLError?
The Python requests library is widely used for making HTTP requests simply and elegantly. However, when working with SSL (Secure Sockets Layer) connections, users may occasionally encounter an SSLError. This error typically arises due to issues with SSL certificates, which are crucial for establishi
5 min read