Multipart form parsing for Falcon.
Project description
[![Build Status](https://travis-ci.org/yohanboniface/falcon-multipart.svg?branch=master)](https://travis-ci.org/yohanboniface/falcon-multipart)
# 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`](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
# 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`](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
falcon-multipart-0.2.0.tar.gz
(3.3 kB
view hashes)
Built Distribution
Close
Hashes for falcon_multipart-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 134c88c2b50b343d93e1b0f2c73e0ce07a89928658af71abc9629f42331a189d |
|
MD5 | 4a2b0ac85ffb60e40660fe9eaf642807 |
|
BLAKE2b-256 | e1a2e50ffc3101ed6b91d1edc63b3586c424ef8071e1ef0ef7dcb8745e65fc14 |