Codebase list python-webargs / master docs / index.rst
master

Tree @master (Download .tar.gz)

index.rst @masterview markup · raw · history · blame

webargs

Release v|version|. (:doc:`Changelog <changelog>`)

System Message: INFO/1 (<string>, line 5)

No role entry for "doc" in module "docutils.parsers.rst.languages.en". Trying "doc" as canonical role name.

System Message: ERROR/3 (<string>, line 5); backlink

Unknown interpreted text role "doc".

webargs is a Python library for parsing and validating HTTP request objects, with built-in support for popular web frameworks, including Flask, Django, Bottle, Tornado, Pyramid, Falcon, and aiohttp.

Upgrading from an older version?

See the :doc:`Upgrading to Newer Releases <upgrading>` page for notes on getting your code up-to-date with the latest version.

System Message: INFO/1 (<string>, line 12)

No role entry for "doc" in module "docutils.parsers.rst.languages.en". Trying "doc" as canonical role name.

System Message: ERROR/3 (<string>, line 12); backlink

Unknown interpreted text role "doc".

Usage and Simple Examples

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__":
    app.run()

# curl http://localhost:5000/\?name\='World'
# Hello World

By default Webargs will automatically parse JSON request bodies. But it also has support for:

System Message: INFO/1 (<string>, line 43)

Possible title underline, too short for the title. Treating it as ordinary text because it's so short.

Query Parameters

System Message: ERROR/3 (<string>, line 44)

Unexpected indentation.
$ curl http://localhost:5000/\?name\='Freddie'
Hello Freddie

# pass location="query" to use_args

System Message: INFO/1 (<string>, line 50)

Possible title underline, too short for the title. Treating it as ordinary text because it's so short.

Form Data

$ curl -d 'name=Brian' http://localhost:5000/
Hello Brian

# pass location="form" to use_args

System Message: INFO/1 (<string>, line 58)

Possible title underline, too short for the title. Treating it as ordinary text because it's so short.

JSON Data

$ curl -X POST -H "Content-Type: application/json" -d '{"name":"Roger"}' http://localhost:5000/
Hello Roger

# pass location="json" (or omit location) to use_args

and, optionally:

  • Headers
  • Cookies
  • Files
  • Paths

Why Use It

  • Simple, declarative syntax. Define your arguments as a mapping rather than imperatively pulling values off of request objects.
  • Code reusability. If you have multiple views that have the same request parameters, you only need to define your parameters once. You can also reuse validation and pre-processing routines.
  • Self-documentation. Webargs makes it easy to understand the expected arguments and their types for your view functions.
  • Automatic documentation. The metadata that webargs provides can serve as an aid for automatically generating API documentation.
  • Cross-framework compatibility. Webargs provides a consistent request-parsing interface that will work across many Python web frameworks.
  • marshmallow integration. Webargs uses marshmallow under the hood. When you need more flexibility than dictionaries, you can use marshmallow Schemas <marshmallow.Schema> to define your request arguments.

Get It Now

pip install -U webargs

Ready to get started? Go on to the :doc:`Quickstart tutorial <quickstart>` or check out some examples.

System Message: INFO/1 (<string>, line 89)

No role entry for "doc" in module "docutils.parsers.rst.languages.en". Trying "doc" as canonical role name.

System Message: ERROR/3 (<string>, line 89); backlink

Unknown interpreted text role "doc".

User Guide

System Message: INFO/1 (<string>, line 94)

No directive entry for "toctree" in module "docutils.parsers.rst.languages.en". Trying "toctree" as canonical directive name.

System Message: ERROR/3 (<string>, line 94)

Unknown directive type "toctree".

.. toctree::
    :maxdepth: 2

    install
    quickstart
    advanced
    framework_support
    ecosystem

API Reference

System Message: INFO/1 (<string>, line 106)

No directive entry for "toctree" in module "docutils.parsers.rst.languages.en". Trying "toctree" as canonical directive name.

System Message: ERROR/3 (<string>, line 106)

Unknown directive type "toctree".

.. toctree::
    :maxdepth: 2

    api


Project Info

System Message: INFO/1 (<string>, line 115)

No directive entry for "toctree" in module "docutils.parsers.rst.languages.en". Trying "toctree" as canonical directive name.

System Message: ERROR/3 (<string>, line 115)

Unknown directive type "toctree".

.. toctree::
   :maxdepth: 1

   license
   changelog
   upgrading
   authors
   contributing

Docutils System Messages

System Message: ERROR/3 (<string>, line 5); backlink

Undefined substitution referenced: "version".