Parsing REST API Payload and Query Parameters With Flask.

Ahmed Nafies
The Startup
Published in
3 min readJun 17, 2020

--

The easiest way for parsing request payload and query parameters

Intro

For a very long time Flask was my first choice of framework when it came to building micro-services (until python 3.6 and async/await came to life). I have used Flask with a different number of extensions depending on the situation. flask-restful, flask-restplus , webargs .. etc. All of these extensions were great until I needed to parse the request payload or query parameters, it gets painful and the reason is they are all built on top of marshmallow.

Marshmallow

Marshmallow is probably the oldest python package for object serialisation and parsing and probably most of the flask extensions are built of top of it.

here is an example of marshmallow

from datetime import date
from pprint import pprint

from marshmallow import Schema, fields


class ArtistSchema(Schema):
name = fields.Str()


class AlbumSchema(Schema):
title = fields.Str()
release_date = fields.Date()
artist = fields.Nested(ArtistSchema())


bowie = dict(name="David Bowie")
album = dict(artist=bowie, title="Hunky Dory", release_date=date(1971, 12, 17))

schema = AlbumSchema()
result = schema.dump(album)
pprint(result, indent=2)
# { 'artist': {'name': 'David

--

--