假设返回一个 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(47)- 通过 Response 自定义响应的类型

 

相关文章:

  • 2021-09-24
  • 2021-11-19
  • 2021-07-23
  • 2022-01-10
  • 2021-11-04
  • 2022-12-23
  • 2021-05-16
  • 2021-06-25
猜你喜欢
  • 2022-02-01
  • 2022-12-23
  • 2021-12-09
  • 2021-07-01
  • 2021-10-07
  • 2021-10-21
  • 2021-09-07
相关资源
相似解决方案