Software

How to Upload Files Using Python Requests

Upload Files Using Python

Numerous libraries that facilitate data transport via HTTP are provided by Python. Due to its widespread use in web scraping, the requests library is one of the most well-liked Python tools. It is also well-liked for communicating with servers. The library not only makes it simple to upload files but also data in well-liked formats like JSON.

File uploading using Python can be carried out using numerous methods, either by using python requests, python-CGI environment or Python SDK etc.

In this article, we will demonstrate how to upload files using the requests module in Python. The post() method signature and the requests library will be discussed first in the article. The procedure for utilizing the requests package to upload a single file will next be discussed. Not least, we upload several files with a single request.

If you’d also like to see how easy it is to implement file uploading functionality using the Python SDK you can learn more about that here.

Single File Upload Using Python’s Requests Library

This part focuses on sending the files; their creation is unimportant. Make three files with the names my_file.txt, my_file2.txt, and my_file3.txt to follow along.

Installing the request library in our workspace is the first thing we must do. It is advised that you install libraries in a virtual environment, even though it is not required:

$ python3 -m venv .

Activate the virtual environment so that we can stop affecting the Python installation globally:

$ . bin/activate

Let’s now use pip to install the requests library:

$ pip install requests

To store our code, create a new file called single_uploader.py. Let’s start by importing the requests library into that file:

import requests

We are now prepared to upload a file! We must open the file and stream the content before posting it. Furthermore, we can’t upload a file that we don’t have access to. The open() function will be used for this.

The file path and the mode are the two parameters that the open() function accepts. The file’s path might be either an absolute path or a relative path to the location where the script is being executed. You can simply use the file’s name if you’re uploading a file that is located in the same directory.

The “read binary” value, denoted by rb, will be accepted by the second argument, mode. This option informs the computer that we want to open the file in read-only mode and that we want to use its binary data:

test_file = open(“my_file.txt”, “rb”)

Please take note that reading the file in binary mode is crucial. The Content-Length header, which is a number in bytes, is often determined by the requests library. The library might receive an inaccurate number for Content-Length if the file is not read in bytes mode, which would result in issues when the file is submitted.

We’ll send requests to the free httpbin service in this example. Developers can test their HTTP requests using this API. Let’s make a variable to hold the web address where we’ll upload our files:

test_url = “http;//httpbin.org/post”

Everything is now ready for us to submit the request. To upload the file, we’ll make use of the requests library’s post() method. The server’s URL and the files property are the two inputs required to make this function. Write the following code to save the response in a variable:

test_response = requests.post(test_url, files = {“form_field_name”: test_file})

A dictionary is accepted by the files property. The name of the form field that will accept the file is the key. Bytes from the opened file that you want to upload make up the value.

Typically, we look at the HTTP status code of the response to determine whether your post() operation was successful. We can take advantage of the response object’s test_url ok field. If so, we’ll print the HTTP server’s answer, which in this case will simply echo the request:

if test_response.ok:
    print(“Upload completed successfully!”)
    print(test_response.text)
else:
    print(“Something went wrong!”)

Let’s give it a shot! Use the python command to run your script in the terminal:

python single_uploader.py

Your output would be similar to this:

Upload completed successfully!
{
  “args”: {},
  “data”: “”,
  “files”: {
    “form_field_name”: “This is my file\nI like my file\n”
  },
  “form”: {},
  “headers”: {
    “Accept”: “*/*”,
    “Accept-Encoding”: “gzip, deflate”,
    “Content-Length”: “189”,
    “Content-Type”: “multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8”,
    “Host”: “httpbin.org”,
    “User-Agent”: “python-requests/2.25.0”,
    “X-Amzn-Trace-Id”: “Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b”
  },
  “json”: null,
  “origin”: “102.5.105.200”,
  “url”: “http;//httpbin.org/post”
}

You can make sure the form_field_name value corresponds to the value in your file as a safety check.

Uploading Multiple Files with Python’s requests Library

The main difference between utilizing requests to upload several files and uploading a single file is that we use lists. Make the following setup code in a new file called multi_uploader.py:

import requests

test_url = “http;//httpbin.org/post”

Now make a dictionary-like variable named test_files that has a list of names and files:

test_files = {
    “test_file1”: open(“my_file.txt”, “rb”),
    “test_file2”: open(“my_file2.txt”, “rb”),
    “test_file3”: open(“my_file3.txt”, “rb”)
}

As previously stated, the form fields’ names serve as the keys, while the files’ sizes serve as the values.

Our file variables can also be created as a list of tuples. Each tuple includes the name of the file-accepting form field and the size of the file in bytes:

test_files = [(“test_file1”, open(“my_file.txt”, “rb”)),
              (“test_file2”, open(“my_fil_2.txt”, “rb”)),
              (“test_file3”, open(“my_file3.txt”, “rb”))]

Choose whichever one you choose because both are effective!

You can send the request and inspect the answer once the list of files is ready:

test_response = requests.post(test_url, files = test_files)

if test_response.ok:
    print(“Upload completed successfully!”)
    print(test_response.text)
else:
    print(“Something went wrong!”)

Execute this script with the python command:

$ python multi_uploader.py

You’ll see this output:

Upload completed successfully!
{
  “args”: {},
  “data”: “”,
  “files”: {
    “test_file1”: “This is my file\nI like my file\n”,
    “test_file2”: “All your base are belong to us\n”,
    “test_file3”: “It’s-a me, Mario!\n”
  },
  “form”: {},
  “headers”: {
    “Accept”: “*/*”,
    “Accept-Encoding”: “gzip, deflate”,
    “Content-Length”: “470”,
    “Content-Type”: “multipart/form-data; boundary=4111c551fb8c61fd14af07bd5df5bb76”,
    “Host”: “httpbin.org”,
    “User-Agent”: “python-requests/2.25.0”,
    “X-Amzn-Trace-Id”: “Root=1-5fc3c744-30404a8b186cf91c7d239034”
  },
  “json”: null,
  “origin”: “102.5.105.200”,
  “url”: “http;//httpbin.org/post”
}

Well done! With requests, you can upload both single and many files!

Conclusion

In this tutorial, we learned how to use the requests module to upload files in Python. The post() method just requires a few adjustments, regardless of whether there are one or more files involved. To make sure that our uploads were successful, we also double-checked our answers.

Comments
To Top

Pin It on Pinterest

Share This