yohanboniface / falcon-multipart

[Experimental] Parse multipart/form-data in falcon
MIT License
58 stars 11 forks source link

Build Status

Parse multipart/form-data requests in Falcon

Install

pip install falcon-multipart

Usage

Add the MultipartMiddleware to your api middlewares:

from falcon_multipart.middleware import MultipartMiddleware

api = falcon.API(middleware=[MultipartMiddleware()])

This will parse any multipart/form-data incoming request, and put the keys in req._params, including files, so you get the field as other params.

Dealing with files

Files will be available as cgi.FieldStorage, with following main parameters:

Example

# Say you have a form with those fields:
# - title => a string
# - image => an image file

def on_post(req, resp, **kwargs):
    title = req.get_param('title')
    image = req.get_param('image')
    # Read image as binary
    raw = image.file.read()
    # Retrieve filename
    filename = image.filename