Handling POSTed multipart/form-data file

2.6k views Asked by At

I'm wondering what is the best way to handle POSTed raw data on the server side. So I'm using Falconframework and I'm able to receive user submitted file

-----------------------------1209846671886287098156775745
Content-Disposition: form-data; name="qquuid"

d3ad452e-a287-4cb7-ac1f-f0a5cdb54386
-----------------------------1209846671886287098156775745
Content-Disposition: form-data; name="qqfilename"

Screenshot.png
-----------------------------1209846671886287098156775745
Content-Disposition: form-data; name="qqtotalfilesize"

1951677
-----------------------------1209846671886287098156775745                                                                                         
Content-Disposition: form-data; name="qqfile"; filename="Screenshot.png"
Content-Type: image/png

�PNG
.................lots of bites............

Using python and hopefully some other lib i would like to turn it into some sort of file object which i can extract metadata - filename , uuid etc, as well as the file itself. Which lib should i use?

2

There are 2 answers

0
Vytas On

Just a very late followup to this old discussion.

As of Falcon 3.0, the framework supports multipart/form-data natively for both WSGI and ASGI applications.

1
vikingben On

Here is a middle ware project that looks promising I'm currently trying to implement this myself in a falcon service.

falcon-multipart

I have have pretty good luck as well using cgi.FeildStorage(). As found in the following post. cgi article

import cgi

def on_post(req, resp):
    env = req.env
    env.setdefault('QUERY_STRING','')
    form = cgi.FieldStorage(fp=req.stream,environ=env)
    form['fileinputname'].file

If you are willing to have one non falcon hook here is an example with bottle: example