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 And Download Files From AWS S3 Using Python?
Pre-requisite: AWS and S3 Amazon 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 ge
3 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
response.history - Python requests
Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain features such as content, headers, etc. This article revolves
2 min read
Building CLI to check status of URL using Python
In this article, we will build a CLI(command-line interface) program to verify the status of a URL using Python. The python CLI takes one or more URLs as arguments and checks whether the URL is accessible (or)not. Stepwise ImplementationStep 1: Setting up files and Installing requirements First, cre
4 min read
Create API Tester using Python Requests Module
Prerequisites: Python Requests module, API In this article, we will discuss the work process of the Python Requests module by creating an API tester. API stands for Application Programming Interface (main participant of all the interactivity). It is like a messenger that takes our requests to a syst
3 min read