Easy way to convert Flask request form, header, JSON and parameters to route arguments.
Project description
flask-request-arg
The easy way to convert Flask request, form, header and request args to route parameters.
Installation
pip install flask-request-arg
Introduction
Handling form
and request
and header
parameters in Flask
is complex and error-prone. Common
issues are:
- Values need to be converted to the correct type.
- Intricate logic used to handle defaults and missing values.
- Request arguments and form fields are not clear from the method signature.
- GET, PUT, POST all require different logic to get values.
- Documentation for request and form values are not easy to generate.
flask-request-arg
solves this issues by allowing you to use a simple decorator
to specify the argument name, type and default value. Then any form data, JSOM
data, header or request argument is converted into a named method parameter. POST using form
data, GET using arguments or PUT with JSON body data all can use the same
code logic.
When using JSON data it is recommended to set the content type to "application/json". An attempt is made to try to convert body data to JSON if no argument is found.
Argument names
Argument names are converted into a Python acceptable variable name by removing white space from each end and then making it lowercase and replacing hyphen, space and other weird characters with underscore _. If the name starts with a number then an underscore is prepended to the name. Example
@request_arg('Header-Value')
becomes:
def route_name(header_value)
General Usage
@request_arg(arg_name: str, arg_type: Any = None, arg_default=None) -> Callable:
arg_name
- the name of the argument to add as a method parameter.arg_type
- the type of the argument. All form and request args are usually strings.arg_default
- default value of the argument when not in form or request.
Notes
- to make an argument required do not provide an
arg_default
. - a
<form>
<input>
name
must match therequest_arg
argument name. - a JSON body key must match the
request_arg
argument name. - any request argument name must be a valid
Python
variable name.
Example
To call an area of circle method with a parameter argument as in this example:
/area_of_circle?radius=23.456
# 1727.57755904
Structure your Flask route as follows:
from flask_request_arg import request_arg
from flask import Response
@request_arg('radius', float)
@app.route('/area_of_circle', methods=['GET'])
def area_of_circle(radius):
result = radius * radius * 3.14
return Response(f"{result}", 200)
Forms
A method that handles POST can be structured the same as a GET. Example:
from flask_request_arg import request_arg
from flask import Response
@request_arg('radius', float)
@app.route('/area_of_circle', methods=['POST'])
def area_of_circle(radius):
result = radius * radius * 3.14
return Response(f"{result}", 200)
HTML example:
<form action="/area_of_circle" method="post">
<label>Radius:<input name="radius" type="number"/></label>
<button type="submit">Get area</button>
</form>
NOTE: the form input name must match the request_arg
argument name.
NOTE: request arguments and form data can be used together on the same request.
JSON Data
JSON body data is treated the same as a POST or GET. Example:
from flask_request_arg import request_arg
from flask import Response
@request_arg('radius', float)
@app.route('/area_of_circle', methods=['PUT'])
def area_of_circle(radius):
result = radius * radius * 3.14
return Response(f"{result}", 200)
Called like:
fetch('/area_of_circle', {
headers: { 'Content-Type': 'application/json' }, // tells the server we have json
method:'PUT',
body: JSON.stringify({radius:45.67}), // json is sent to the server as text
})
NOTE: request arguments and JSON body data can be used together on the same request.
As you can see the Flask
method code is the same for GET, PUT and POST. So you can
do all three at once. Example:
from flask_request_arg import request_arg
from flask import Response
@request_arg('radius', float)
@app.route('/area_of_circle', methods=['GET', 'PUT', 'POST'])
def area_of_circle(radius):
result = radius * radius * 3.14
return Response(f"{result}", 200)
Request arguments
Request arguments of the type
/route?argument1=value1&argument2=value2
are treated the same as form
or JSON
data. Example:
from flask_request_arg import request_arg
from flask import Response
# /area_of_circle?radius=124.56
@request_arg('radius', float)
@app.route('/area_of_circle')
def area_of_circle(radius):
result = radius * radius * 3.14
return Response(f"{result}", 200)
Request headers
Request headers of the format:
header-name: values here and here
are treated the same as form
, parameters
or JSON
data.
Converting values
Use the arg_type
parameter to specify a type conversion for the string value.
The arg_type can be any Python type. The default isstr
. Example:
from flask_request_arg import request_arg
from flask import Response
@request_arg('radius', float)
@request_arg('number_of_circles', int)
@request_arg('name', str)
@app.route('/area_of_circle', methods=['GET'])
def area_of_circle(radius, number_of_circles, name):
result = number_of_circles * radius * radius * 3.14
return Response(f"{number_of_circles} of {name} is {result}", 200)
Custom type converters can be supplied using a lambda
. Example:
@request_arg("arg_type", lambda x: x == "True")
@app.route('/custom')
def custom_arg_type(arg_type):
result = "yes" if arg_type else "no"
return Response(f"{result}", 200)
When using bool
as an arg_type
, a truthy test will be done and
return True
if the value is in:
("y", "Y", "yes", "Yes", "YES", True, "true", "True", "TRUE", 1, "1")
Mixing parameters
If required, you can mix Flask request parameters with request arguments. Example:
from flask_request_arg import request_arg
from flask import Response
@request_arg('radius', float)
@app.route('/area_of_circle/<float:pi>/', methods=['GET'])
def area_of_circle(pi, radius):
result = radius * radius * pi
return Response(f"{result}", 200)
Release history
- 1.0.5 - Allow JSON coercion of body data.
- 1.0.4 - Add argument variable name fixing
- 1.0.3 - Add header support
- 1.0.2 - Fix publish
- 1.0.1 - Use truthy values for
bool
types - 1.0.0 - Tidy up documentation. Proper release.
- 0.0.2 - Initial release
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flask-request-arg-1.0.5.tar.gz
.
File metadata
- Download URL: flask-request-arg-1.0.5.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c10f2f2b9004a3b4d328c56b60250f98a6dbcbf1e8ac9e9a243defe7828f0a2 |
|
MD5 | 68c435c284fe5358e36959b8719fed77 |
|
BLAKE2b-256 | 9be1f3c50373e0c826fab2d21302f1106128778d6aa88f7412345cf0b76ff31a |
File details
Details for the file flask_request_arg-1.0.5-py2.py3-none-any.whl
.
File metadata
- Download URL: flask_request_arg-1.0.5-py2.py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cefaf18bb94c62fa6957b936c2dce273354e4f8dae41205729b8df4488072d05 |
|
MD5 | 6ef99a9cdf5313eb0a1b209a30c2deae |
|
BLAKE2b-256 | 42540c44f786d721e511ecd2a90a9fcd06c2cf859da247be9e22e6d0a4a90c14 |