其他分享
首页 > 其他分享> > FastAPI(47)- 通过 Response 自定义响应的类型

FastAPI(47)- 通过 Response 自定义响应的类型

作者:互联网

假设返回一个 XML 格式的响应

import uvicorn
from fastapi import FastAPI, Response

app = FastAPI()


@app.get("/legacy/")
def get_legacy_data():
    data = """<?xml ve rsion="1.0"?>
    <shampoo>
    <Header>
        Apply shampoo here.
    </Header>
    <Body>
        You'll have to use soap here.
    </Body>
    </shampoo>
    """
    # 重点就是指定 media_type
    return Response(content=data, media_type="application/xml")


if __name__ == '__main__':
    uvicorn.run(app="39_responses:app", reload=True, host="127.0.0.1", port=8080)

 

请求结果

 

标签:__,自定义,FastAPI,app,legacy,47,data,Response
来源: https://www.cnblogs.com/poloyy/p/15364510.html