adafruit / Adafruit_CircuitPython_Requests

Requests-like interface for web interfacing
MIT License
51 stars 37 forks source link

File uploads and chunked Transfer-Encoding #141

Open mycoporium opened 1 year ago

mycoporium commented 1 year ago

I am using a TTL serial camera with a board that has no storage, but does have wifi (the M7 metro).

The example code buffers the image from the camera 32 bytes at a time for writing to the SD card. But I was hoping to utilize the Transfer-Enoding: chunked header to send the buffer data with an HTTP request in place of writing to disk.

It doesn't look like the circuitpython requests library supports basic file uploads, let alone a chunked method.

Here is a link to the python-requests docs showing how to do streaming file uploads and chunked body requests: https://requests.readthedocs.io/en/latest/user/advanced/#streaming-uploads

This is a working chunked encoding example that I'm using to test this functionality against a CherryPy server (not shown), which verifies that python-requests can do what it says.

import requests

IMG_FILE = 'image_18000.jpg'

def read_chunks(fileobj, chunk_size=32):

    while True:
        data = fileobj.read(chunk_size)
        if not data:
            break

        yield data

if __name__ == '__main__':

    with open(IMG_FILE, 'rb') as f:
        resp = requests.post('http://hb.local:9001', data=read_chunks(f, 32))

    print(resp.json())

If I have some time, I will try to create a PR for this functionality.

There is also a related issue here: #89

FoamyGuy commented 5 months ago

Basic file uploading is implemented in #189. Contribution of the chunked Transfer-Encoding functionality is welcomed for anyone interested in working on it.