About parameter processing in Flask request.args

If you know it, you can enjoy it, so I want people who use Flask to remember it.

The actual state of flask.request.args is the following instance when reading the source.

werkzeug.datastructures.ImmutableMultiDict

I think the most used method in request.args is probably get, but the reality is

from werkzeug.datastructures import TypeConversionDict

Get method of.

You can specify the type by reading the help of this.

 |  ----------------------------------------------------------------------
 |  Methods inherited from TypeConversionDict:
 |  
 |  get(self, key, default=None, type=None)
 |      Return the default value if the requested data doesn't exist.
 |      If `type` is provided and is a callable it should convert the value,
 |      return it or raise a :exc:`ValueError` if that is not possible.  In
 |      this case the function will return the default as if the value was not
 |      found:
 |      
 |      >>> d = TypeConversionDict(foo='42', bar='blub')
 |      >>> d.get('foo', type=int)
 |      42
 |      >>> d.get('bar', -1, type=int)
 |      -1
 |      
 |      :param key: The key to be looked up.
 |      :param default: The default value to be returned if the key can't
 |                      be looked up.  If not further specified `None` is
 |                      returned.
 |      :param type: A callable that is used to cast the value in the
 |                   :class:`MultiDict`.  If a :exc:`ValueError` is raised
 |                   by this callable the default value is returned.
 |  
 |  ----------------------------------------------------------------------

In other words, this parameter can be easily specified as an INT type.

To give a simple example, I think that offset and limit parameters are often created in the reference API, but it can be done in the following two lines including the type specification.

    offset = request.args.get("offset", default=0, type=int)
    limit = request.args.get("limit", default=10, type=int)

There are quite a few things you won't notice just by reading the Flask documentation, so if you read the werkzeug documentation and implementations, you'll find something unexpected, so be sure to read it.

If you extend this further, you can also receive parameters using your own data type.

class SortType(object):
    accepts = ["-time", "-size", "+time", "+size"]    
    default = ["-time"]

    def __init__(self, sort_parameter):
        super(SortType, self).__init__()    
        if sort_parameter in self.accepts:
           self.sort = sort_parameter
        else:
           self.sort = self.default

    def __repr__(self):
        return self.sort

    def __str__(self):
        return self.sort
sort = '%s' % request.args.get("sort", type=SortType)

We define a class with some sort types and a default sort, define it in type and receive the sort parameter.

Recommended Posts

About parameter processing in Flask request.args
Celery asynchronous processing in Flask
About Flask customization
I investigated in detail about variable processing in python
About processing IoT time series data-Aggregation processing in Azure Time Series Insights
File processing in Python
Multithreaded processing in python
Text processing in Python
Queue processing in Python
About __all__ in python
Image uploader in Flask
UTF8 text processing in python
Use parameter store in Python
Image Processing Collection in Python
Simply check Content-Type in Flask (@content_type)
Using Python mode in Processing
Transfer parameter values in Python
About "for _ in range ():" in python
Upload multiple files in Flask