Multipart form parsing for Falcon.
Project description
# Parse multipart/form-data requests in Falcon
## Install
No pypi release yet, so install from git:
pip install git+https://github.com/yohanboniface/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`](https://docs.python.org/3/library/cgi.html),
with following main parameters:
- `file`: act as a python file, you can call `read()` on it, and you will
retrieve content (as *bytes*)
- `filename`: the filename, if given
- `value`: the file content in *bytes*
- `type`: the content-type, or None if not specified
- `disposition`: content-disposition, or None if not specified
## 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
## Install
No pypi release yet, so install from git:
pip install git+https://github.com/yohanboniface/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`](https://docs.python.org/3/library/cgi.html),
with following main parameters:
- `file`: act as a python file, you can call `read()` on it, and you will
retrieve content (as *bytes*)
- `filename`: the filename, if given
- `value`: the file content in *bytes*
- `type`: the content-type, or None if not specified
- `disposition`: content-disposition, or None if not specified
## 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file falcon-multipart-0.1.0.tar.gz
.
File metadata
- Download URL: falcon-multipart-0.1.0.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e01e19271f4f587cbdfa996050934cb74f5e7bb1a6d0322d6bc263226e03711 |
|
MD5 | 72cf72766c28ccba04c1fbb04d4a4eda |
|
BLAKE2b-256 | c287b848c11b501c71d2ebba497640c44e1426a4a7a8875022470d92a59dc9bc |