Skip to main content

Convert Flask's `request.get_json` dict into a MultiDict like `request.form`

Project description

Build Status

Convert Flask’s request.get_json dict into a MultiDict like request.form

This was written to maintain a consistent API for interacting with both request.form and request.get_json(). This allows use to leverage .get with type coercion and .getlist.

Getting Started

Install the module with: pip install flask_json_multidict

# Load in our dependencies
from flask import Flask, request, jsonify
from flask_json_multidict import get_json_multidict

# Start an application
app = Flask(__name__)

def resolve_request_body():
    """Before every request, resolve `request.body` from either `request.form` or `request.get_json()`"""
    request.body = request.form
    if request.headers['content-type'] == 'application/json':
        request.body = get_json_multidict(request)
app.before_request(resolve_request_body)

@app.route('/', methods=['POST'])
def root():
    """Reply with POST data as we see it"""
    body = request.body
    return jsonify({key: body[key] for key in body})
    # We can also leverage `request.body.getlist` as we do with `request.form`


if __name__ == '__main__':
    app.run()

# $ curl http://localhost:5000/ -X POST --data 'hello=world'
# {"hello": "world"}
# $ curl http://localhost:5000/ -X POST -H 'Content-Type: application/json' --data '{"hello": "world"}
# {"hello": "world"}

Documentation

flask-json-multidict can be imported via flask_json_multidict.

flask_json_multidict.get_json_multidict(request)

get_json_multidict walks over the json provided by request.get_json() and returns a MultiDict.

  • request object - Current request being handled by Flask

Returns:

  • body object - MultiDict with json information
    • If there were any dictionaries or nested lists, then these will be ignored as parameters
      • This is for consistency with how request.form behaves

# Assume we receive `{"colors": ["red", "blue"]}`
body = get_json_multidict(request)
body.getlist('colors')  # ['red', 'blue']

# Assume we receive `{"hello": "world"}`
body = get_json_multidict(request)
body.['hello']  # 'world'
body.get('hello')  # 'world'

# Assume we receive `{"foo": {"bar": "baz"}}`
# This is the silent ignore of bad parameters
body = get_json_multidict(request)
body.get('foo')  # None

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Test via nosetests.

License

Copyright (c) 2015 Underdog.io

Licensed under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

flask_json_multidict-1.0.0.zip (24.0 kB view details)

Uploaded Source

flask_json_multidict-1.0.0.tar.gz (11.0 kB view details)

Uploaded Source

File details

Details for the file flask_json_multidict-1.0.0.zip.

File metadata

File hashes

Hashes for flask_json_multidict-1.0.0.zip
Algorithm Hash digest
SHA256 c45c25d1c2a439999626c645daa8dfc251de4a1e0718de3eb118b57ca9125495
MD5 1235a4c1ec9b1df3754c40e1bc8eb8e1
BLAKE2b-256 f214f301f084d3ad70b995a7fe62e1aa3caacc460e4b810ff0e8d6868805318b

See more details on using hashes here.

File details

Details for the file flask_json_multidict-1.0.0.tar.gz.

File metadata

File hashes

Hashes for flask_json_multidict-1.0.0.tar.gz
Algorithm Hash digest
SHA256 26234ff1dd6020b0c2d671b85b2bd3887724be6e18aa36957ede332f9f6ef6b7
MD5 7d7eece96d522ad7847113d9bf5b25fc
BLAKE2b-256 47d4ee55bc2e7e1608c563b2dc61bb45c4807c5dfc826a777b4a9085b0f3533f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page