Advertisements

How to upload a file and some data through HTTP multipart in Python 3 using the requests library

Undeniably, the HTTP protocol had become the dominant communication protocol between computers.

For example, when we want to upload a file and some data from to a HTTP server, we do so through a HTTP multipart request.

So how can we do that in Python? In Python 3, we can use the services of the requests library to help us construct HTTP multipart request easily.

In case you need it, this is how to upload a file and some data through HTTP multipart in Python 3 using the requests library.

Defining a sample scenario

Let's assume that you are building a Python 3 application for your Raspberry Pi 3. Whenever, a button is pressed, an image will be captured with the camera on your Raspberry Pi along with the temperature from a sensor. The image will be available in the filesystem of your Raspberry Pi 3.

Today you will be building the function that will take a file path to an image and a float value for the temperature. Inside the function, you will upload the image file and the temperature to a server endpoint at http://www.example.com/api/v1/sensor_data.

Installing the requests library

In order to use the requests library, you need to install it by running the following pip command inside your Python environment:

pip install requests

Writing the Python 3 function to upload the image file and temperature to the server endpoint

Once you had installed the Python 3 dependency for the requests library, let's write the function to send the HTTP multipart request:

import requests
import os

function send_data_to_server(image_path, temperature):
        
    image_filename = os.path.basename(image_path)

    multipart_form_data = {
        'image': (image_filename, open(image_path, 'rb')),
        'temperature': ('', str(temperature)),
    }

    response = requests.post('http://www.example.com/api/v1/sensor_data/',
                             files=multipart_form_data)

    print(response.status_code)

The send_data_to_server function accepts a path to an image and a temperature value as its input value. Once the function is being called, it first get the filename of the image from the path.

After that, it constructs a dictionary with two multipart variables - image and temperature. Both variables and their values are represented by 2-tuples. The image variable is assigned a filename and a file handle to the actual image. On the other hand, as temperature is a single value, the first element of the 2-tuple is empty while the second element contains the temperature as a string.

Once we had constructed the dictionary to represent the content, we call requests.post to send a HTTP multipart request to the server endpoint at http://www.example.com/api/v1/sensor_data/.

Lastly, the function prints the status of the HTTP response received from the server.

Advertisements

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.

Advertisements
Advertisements

Udemy.com Homepage 300x250 Domain names for just 88 cents!

Find a domain starting at $0.88

powered by Namecheap