ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

FastAPI(48)- 自定义响应之 HTMLResponse、PlainTextResponse

2022-01-06 12:34:38  阅读:195  来源: 互联网

标签:PlainTextResponse return 自定义 HTMLResponse FastAPI app response Response


FastAPI(48)- 自定义响应之 HTMLResponse、PlainTextResponse 

 

背景

  • 上一篇文章讲了通过 Response 自定义响应,但有一个缺点
  • 如果直接返回一个 Response,数据不会自动转换,也不会显示在文档中
  • 这一节开始讲自定义响应

 

会讲解多个响应类型

所有响应类都是继承于 Response

 

HTMLResponse

作用

返回一些 HTML 代码

 

实际代码

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    <html>
        <head>
            <title>Some HTML in here</title>
        </head>
        <body>
            <h1>Look ma! HTML!</h1>
        </body>
    </html>
    """

上面的栗子中,Response Header 的 Content-type 将为 text/html,并且会记录在 OpenAPI 中

 

查看 Swagger API 文档的 Response Header

 

请求结果

 

源码

 

只是声明了下 media_type,其他都没变

 

返回自定义 Response 的第二种方式

背景

  • 上面的两个栗子是通过在路径操作装饰器的 response_class 来声明 Response  @app.get("/items/", response_class=HTMLResponse) 
  • 下面的栗子将会讲解在路径操作函数中直接 return Response

 

实际代码

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    <html>
        <head>
            <title>Some HTML in here</title>
        </head>
        <body>
            <h1>Look ma! HTML!</h1>
        </body>
    </html>
    """
    # 直接返回 HTMLResponse
    return HTMLResponse(content=html_content, status_code=200)
  • 这样的写法效果是等价于上一个栗子的写法
  • 但这样写有个缺点,开头也说了直接返回 Response 的缺点
  • 不会记录在 OpenAPI 中,比如不会记录 Content-type,并且不会在 Swagger API 文档中显示

 

查看 Swagger API 文档的 Response Header

 

请求结果

 

添加 response_class 和 return Response 综合使用

上面的栗子讲了直接 return Response 的缺点,那么可以结合使用 response_class 来避开问题

# 1、声明 response_class
@app.get("/items2/", response_class=HTMLResponse)
async def read_items():
    html_content = """
    <html>
        <head>
            <title>Some HTML in here</title>
        </head>
        <body>
            <h1>Look ma! HTML!</h1>
        </body>
    </html>
    """
    # 2、仍然 return HTMLResponse
    return HTMLResponse(content=html_content, status_code=200)

 

PlainTextResponse

作用

返回一些纯文本数据

 

实际代码

from fastapi import FastAPI
from fastapi.responses import PlainTextResponse

app = FastAPI()


@app.get("/", response_class=PlainTextResponse)
async def main():
    return "Hello World"

 

查看 Swagger API 文档的 Response Header

 

默认是 application/json,现在改成了 text/plain

 

请求结果

 

源码

只是声明了下 media_type,其他都没变

 

假如直接 return 字符串,Content-type 默认会是什么类型?

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def main():
    return "Hello World"

默认还是 application/json,因为 FastAPI 是使用 JSONResponse 返回响应的

 

 

标签:PlainTextResponse,return,自定义,HTMLResponse,FastAPI,app,response,Response
来源: https://www.cnblogs.com/xiao-xue-di/p/15770545.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有