/Flask-Request-Data-Normalizer

Streamline and supercharge your query string parameter parsing with this Flask extension. Supports base64 encoded query parameters!

Primary LanguagePythonBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Flask Extension: Request Data Normalizer (RDN)

Streamline and supercharge your query string parameter parsing in Flask! This Flask extension provides support for parsing complex Python objects from base64 encoded query parameters, and makes passed query parameterss of any encoding available via Flask's Request.get_json().

Flask Query String Manager PyPi

Installation

pip install Flask-RDN

Quick Start

from flask_request_data_normalizer import RequestDataNormalizer
from flask import Flask

app = Flask(__name__)
RequestDataNormalizer(app)

Overview

In Flask, parameters passed in a URL query string (such as test/val in localhost/?test=val) are accessed in Dictionary form via the Request.args or Request.values properties but not the Request.get_json() method.

This extension adds these parsed query parameters to the results of Request.get_json() making the method a one-stop-shop to retrieve any data passed to an endpoint.

Additionally, this extension provides support for parsing base64 encoded query parameters. This allows complex objects to be encoded in query parameters and automatically parsed by Flask!

Base64 Parsing Examples

Normally nested data like lists or maps (e.g. [1,2,3] or {"a": "b"}) can't be sent as a query parameter to a Flask application. But it can if Flask can read base64 encoded data.

For example, a Javascript application could create the following complex query string and base64 encode it:

var obj = {nested: {a: 'a', b: 'b'}, list: [1, {"in": "list"}, true]};
"?data=" + btoa(JSON.stringify(obj));

'?data=eyJuZXN0ZWQiOnsiYSI6ImEiLCJiIjoiYiJ9LCJsaXN0IjpbMSx7ImluIjoibGlzdCJ9LHRydWVdfQ=='

With this extension, Flask will be able to parse this data into a Python object automatically.

Take this sample Flask application with a single endpoint:

from flask_request_data_normalizer import RequestDataNormalizer
from flask import Flask, request

app = Flask(__name__)
RequestDataNormalizer(app)

@app.route('/')
def endpoint():
   return str(request.get_json())

app.run(host="0.0.0.0", debug=True)

If we called the above endpoint with our base64 encoded query string we would get the following Python object

{'data': {'nested': {'a': 'a', 'b': 'b'}, 'list': [1, {'in': 'list'}, True]}}

The parsing of base64 query strings is done by the Query String Manager library under the hood. Check it out for more examples of how base64 query strings are parsed!

Contributing

Contributions are welcome! Please not the following when contributing:

  • Unittests must be added under the tests/ directory for the PR to be approved. You can run unittests from the root project directory with the following command:

    python setup.py test
  • PRs cannot be merged without all unittests passing (they will execute automatically)

  • Merges to main will automatically create a new release on PyPi unless it is from a forked Repo