Skip to main content

Handle YAML data in FastAPI

Project description

Overview

Handle YAML requests with FastAPI. The package will do the best effort to convert the YAML request to JSON. All files that are uploaded as multipart/form-data will be concatenated and converted to JSON if the header handle-as-yaml is set to true.

Installation

pip install fastapi-yaml

Usage

FastAPI Code:

from fastapi import FastAPI
from fastapi_yaml import YamlRoute
from pydantic import BaseModel

app = FastAPI()
app.router.route_class = YamlRoute

class Person(BaseModel, extra='forbid'):
    name: str
    age: int

@app.post("/person")
def person(person: Person):
    return f"{person.name} is {person.age} years old"

HTTP Request:

curl --request POST \
  --url http://localhost:8000/person \
  --header "content-type: application/x-yaml" \
  --data "name: John Doe\nage: 42"

Tests

poetry run pytest --cov fastapi_yaml tests/

Coverage

---------- coverage: platform darwin, python 3.11.2-final-0 ----------
Name                       Stmts   Miss  Cover
----------------------------------------------
fastapi_yaml/__init__.py       1      0   100%
fastapi_yaml/main.py          20      0   100%
----------------------------------------------
TOTAL                         21      0   100%

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_yaml-0.3.0rc0.tar.gz (2.3 kB view details)

Uploaded Source

Built Distribution

fastapi_yaml-0.3.0rc0-py3-none-any.whl (2.8 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_yaml-0.3.0rc0.tar.gz.

File metadata

  • Download URL: fastapi_yaml-0.3.0rc0.tar.gz
  • Upload date:
  • Size: 2.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for fastapi_yaml-0.3.0rc0.tar.gz
Algorithm Hash digest
SHA256 6afc24379ac70c33a0d0464763902323cff05c99842ebdff352ecbfd6797003a
MD5 e5c485bd67e71b6bec550e75fb0677c6
BLAKE2b-256 7d8b76f69ec1917f09b7dda22c1ba0e0f3b066f741e61a4c44f1b2e8b4761e7c

See more details on using hashes here.

File details

Details for the file fastapi_yaml-0.3.0rc0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_yaml-0.3.0rc0-py3-none-any.whl
Algorithm Hash digest
SHA256 dc91963473989e6e233aeb74c510144beeac394837aff92b5cbe0a082354653b
MD5 7c48093e97171d348300eb7be16e53a1
BLAKE2b-256 4c6951e8f58474a6c00632dc8bc9a9d2263ecc5c3ac3892c7427c468232b144d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page