Skip to main content

Python parser for RESTful HTTP requests

Python package

A simple, lightweight parser for RESTful HTTP request data. Python 3.6+

Installation

pip install restparse

Example usage:

from restparse.parser import Parser


parser = Parser(description="RESTful parameter parser")

parser.add_param(
    name="name",
    type=str,
    description="The users name",
    required=True
)
parser.add_param(
    name="age",
    type=int,
    description="The users age",
    required=True
)
parser.add_param(
    name="online",
    type=bool,
    description="Is the user online?",
    default=False
)
parser.add_param(
    name="height",
    type=float,
    description="The users height",
)
parser.add_param(
    name="tags",
    description="Tags",
)

payload = {
    "name": "John Doe",
    "age": "40",
    "online": False,
    "height": 6.2,
    "tags": ["python", "javascript"]
}

params = parser.parse_params(payload)

print(params.name)  # John Doe
print(params.tags)  # ['python', 'javascript']

print(params.to_dict())  # {'name': 'John Doe', 'age': 40, 'online': False, 'height': 6.2, 'tags': ['python', 'javascript']}

Usage with Flask

Parsing query strings:

@app.route("/")
def index():
    """ Parsing query strings """

    parser = Parser(description="Parsing query strings")

    parser.add_param(
        "q_from",
        type=int,
        description="Query from"
    )
    parser.add_param(
        "q_to",
        type=int,
        description="Query to"
    )
    parser.add_param(
        "search",
        type=str,
        description="Search query"
    )
    params = parser.parse_params(request.args)

    print(params.q_from)
    print(params.q_to)
    print(params.search)

    return f"Params = from: {params.q_from}, to: {params.q_to}, search: {params.search}"

Parsing request payloads:

@app.route("/json", methods=["POST"])
def json_payload():
    """ Parsing request payloads """

    parser = Parser(description="Parsing a request payload")

    parser.add_param(
        "name",
        type=str,
        description="The users name",
        required=True
    )
    parser.add_param(
        "age",
        type=int,
        description="The users age",
        required=True
    )
    parser.add_param(
        "tags",
        type=list,
        description="Tags"
    )
    params = parser.parse_params(request.get_json())

    print(params.name)
    print(params.age)
    print(params.tags)

    return jsonify(params.to_dict())

Parsing form data:

@app.route("/form", methods=["POST"])
def form_payload():
    """ Parsing form data """

    parser = Parser(description="Parsing form data")

    parser.add_param(
        "name",
        type=str,
        description="The users name",
        required=True
    )
    parser.add_param(
        "age",
        type=int,
        description="The users age",
        required=True
    )
    params = parser.parse_params(request.form)

    print(params.name)
    print(params.age)

    return redirect(url_for("index"))

add_param()

parser.add_param(
    "name",
    type=str,
    dest="new_name",
    description="A description of the param",
    required=True,
    choices=["foo", "bar"]
)

options:

name (str): The parameter name
type (type): The type to which the parser should expect
dest (str): The name of the attribute to be added to the object returned by parse_params()
description (str): A description of the param
required (bool): Whether or not the param may be omitted
choices (container): A container of the allowable values for the argument
default: The value produced if the argument is absent from the params

Metadata

Release files for restparse 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for restparse 0.0.7
File Size Uploaded Provenance
restparse-0.0.7.tar.gz 6.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for restparse 0.0.7
File Interpreter ABI Platform
restparse-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 14.8 kB

Release files / restparse-0.0.7.tar.gz

Download URL restparse-0.0.7.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
dd9a649a4e2bb5ce9d269161014efe584ebc4e88a56c59c9e250499887d617d3
BLAKE2b-256 checksum
How to use checksums
4758ff97fc6d993156d8f4ea3fba546de47a7b3c4c2d1b076a98f4c81605d64b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

Release files / restparse-0.0.7-py3-none-any.whl

Download URL restparse-0.0.7-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
166083d30247adb73492fba9452ffefc430329e552f81ec6d2a59c90a12922c0
BLAKE2b-256 checksum
How to use checksums
71a2527b71937f1baac35b6141e8392818a007ca5d3be365eeaeace79275802b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

Security for restparse 0.0.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page