adds xml support to fastapi
Project description
FastAPI-XML
Seamless XML support for FastAPI — parse XML request bodies and return XML responses using standard Python dataclasses, with full OpenAPI integration.
pip install fastapi-xml
Built on xsdata for XML serialization, it slots into FastAPI the same way JSON does.
Features
- XML request bodies — replace
Body()withXmlBody(), your endpoint receives a populated dataclass - XML responses — return any dataclass and get back
application/xml(ortext/xml) - OpenAPI schema — Swagger UI renders XML examples and schemas automatically
- Zero learning curve — works like FastAPI's JSON, just swap in
XmlRouteandXmlBody - Two media types —
XmlAppResponse(application/xml) andXmlTextResponse(text/xml)
Quick Start
from dataclasses import dataclass, field
from fastapi import FastAPI
from fastapi_xml import XmlRoute, XmlAppResponse, XmlBody, add_openapi_extension
@dataclass
class HelloWorld:
message: str = field(metadata={"type": "Element"})
app = FastAPI(title="FastAPI-XML", default_response_class=XmlAppResponse)
app.router.route_class = XmlRoute
add_openapi_extension(app)
@app.post("/echo", response_model=HelloWorld)
def echo(x: HelloWorld = XmlBody()) -> HelloWorld:
x.message += " Forever!"
return x
<!-- Request -->
<HelloWorld><message>Hello</message></HelloWorld>
<!-- Response -->
<HelloWorld><message>Hello Forever!</message></HelloWorld>
How It Works
XmlRoute replaces FastAPI's default APIRoute. When a request arrives with an XML body, the XmlDecoder (wrapping xsdata's XmlParser) deserializes it into your dataclass. On the way out, XmlResponse serializes the return value back to XML via xsdata's XmlSerializer.
Response Types
| Class | Content-Type |
|---|---|
XmlAppResponse |
application/xml |
XmlTextResponse |
text/xml |
OpenAPI
Call add_openapi_extension(app) to generate accurate XML schemas in your OpenAPI spec. The Swagger UI at /docs will show XML examples and let you test endpoints directly.
Example
from dataclasses import dataclass, field
from fastapi import FastAPI
from fastapi_xml import XmlRoute, XmlAppResponse, XmlBody
@dataclass
class Item:
name: str = field(metadata={"type": "Element"})
price: float = field(metadata={"type": "Element"})
@dataclass
class Order:
id: str = field(metadata={"type": "Element"})
items: list[Item] = field(metadata={"type": "Element", "name": "Item"})
app = FastAPI(default_response_class=XmlAppResponse)
app.router.route_class = XmlRoute
@app.post("/order", response_model=Order)
def create_order(order: Order = XmlBody()) -> Order:
return order
if __name__ == "__main__":
import uvicorn
uvicorn.run(app)
API
| Symbol | Description |
|---|---|
XmlBody() |
Mark a parameter as coming from XML body |
XmlRoute |
Route class that enables XML handling |
XmlAppResponse |
Response class for application/xml |
XmlTextResponse |
Response class for text/xml |
add_openapi_extension |
Patches OpenAPI schema for XML types |
Requirements
- Python 3.10+
- FastAPI >= 0.113.0
- xsdata >= 22.9
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_xml-1.1.2.tar.gz.
File metadata
- Download URL: fastapi_xml-1.1.2.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a54eb2e83795edd981e93c4d10862fce956cfe37527555cd1ca9363608b9a1cb
|
|
| MD5 |
f1320fadaeb9b8888cd95803acbe920a
|
|
| BLAKE2b-256 |
150564d218f67f9d5f94a4f2b27796ffd5db28d5965aa1d20ffc3360473beaa3
|
File details
Details for the file fastapi_xml-1.1.2-py3-none-any.whl.
File metadata
- Download URL: fastapi_xml-1.1.2-py3-none-any.whl
- Upload date:
- Size: 15.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
322bf5545cccf7973d9f001652b99c05b1e33a1fc03cf05896c65c7fbf989ae0
|
|
| MD5 |
d6dbc4ac7ff613f25ed8a3c78a55cf75
|
|
| BLAKE2b-256 |
80398d0be531ed24a59b3ac8eb927021b44171d705f61f5cab6e41f23f7e7898
|