接口中返回xml格式内容
from fastapi import FastAPI, Response
app = FastAPI()
# ① xml
@app.get("/legacy")
def get_legacy_data():
data = """<?xml version="1.0"?>
<shampoo>
<Header>
Apply shampoo here.
</Header>
<Body>
You'll have to use soap here.
</Body>
</shampoo>
"""
return Response(content=data, media_type="application/xml")
我们看下实际返回:

返回的类型是xml格式的,说明返回成功。
接口返回中定制headers
@app.get("/legacy_with_headers")
def get_legacy_with_headers_data():
headers = {"X-Xtoken": "LC", "Content-Language": "en-US"}
data = """<?xml version="1.0"?>
<shampoo>
<Header>
Apply shampoo here.
</Header>
<

最低0.47元/天 解锁文章
760

被折叠的 条评论
为什么被折叠?



