webargs

8.7.1 · active · verified Fri Apr 10

webargs is a Python library for declarative parsing and validation of HTTP request objects. It provides built-in support for popular web frameworks, including Flask, Django, Bottle, Tornado, Pyramid, Falcon, and aiohttp. Leveraging Marshmallow under the hood, it offers a consistent API for defining request arguments and handling validation errors across different frameworks. The library is actively maintained, with the current stable version being 8.7.1.

Warnings

Install

Imports

Quickstart

This Flask example demonstrates how to use the `use_args` decorator to parse and validate a 'name' query parameter. It uses `fields.Str` from `webargs` (which re-exports `marshmallow.fields`) to define the expected argument.

from flask import Flask
from webargs import fields
from webargs.flaskparser import use_args

app = Flask(__name__)

@app.route("/")
@use_args({"name": fields.Str(required=True)}, location="query")
def index(args):
    return "Hello " + args["name"]

if __name__ == "__main__":
    # To run: python your_app.py
    # Then try: curl "http://127.0.0.1:5000/?name=World"
    app.run(debug=True)

view raw JSON →