上一页 1 ··· 3 4 5 6 7 8 9 10 11 ··· 140 下一页
摘要: 前言 可以在 路径函数 中定义一个类型为 Response的参数,这样你就可以在这个临时响应对象中设置cookie了。 response 参数 设置cookies from fastapi import FastAPI, Response app = FastAPI() @app.post("/co 阅读全文
posted @ 2023-09-17 11:01 上海-悠悠 阅读(186) 评论(0) 推荐(0)
摘要: 前言 你可以在你的_路径操作函数_中声明一个Response类型的参数。 设置响应头部 你可以在这个_临时_响应对象中设置头部 from fastapi import FastAPI, Response app = FastAPI() @app.get("/headers-and-object/") 阅读全文
posted @ 2023-09-17 11:00 上海-悠悠 阅读(253) 评论(0) 推荐(0)
摘要: 前言 假设你想默认返回一个HTTP状态码为“OK”200。 但如果数据不存在,你想创建它,并返回一个HTTP状态码为“CREATED”201。 但你仍然希望能够使用response_model过滤和转换你返回的数据。 对于这些情况,你可以使用一个response`参数。 使用 response 参数 阅读全文
posted @ 2023-09-17 10:59 上海-悠悠 阅读(256) 评论(0) 推荐(0)
摘要: 前言 假设你想要返回一个 XML 响应。 你可以把你的 XML 内容放到一个字符串中,放到一个 Response 中,然后返回。 Response 自定义返回 可以把 XML 内容放到一个字符串中,放到一个 Response 中,设置media_type="application/xml" from 阅读全文
posted @ 2023-09-17 10:58 上海-悠悠 阅读(282) 评论(0) 推荐(0)
摘要: 前言 通过我们返回JSON类型的接口会比较多,除了返回JSON格式,还可以响应其它格式的内容 JSONResponse Content-Type 会被设置成 application/json HTMLResponse Content-Type 会被设置成 text/html PlainTextRes 阅读全文
posted @ 2023-09-17 10:56 上海-悠悠 阅读(465) 评论(0) 推荐(0)
摘要: 前言 与指定响应模型的方式相同,你也可以在以下任意的_路径操作_中使用 status_code 参数来声明用于响应的 HTTP 状态码: @app.get() @app.post() @app.put() @app.delete() 响应状态码 from fastapi import FastAPI 阅读全文
posted @ 2023-09-17 10:55 上海-悠悠 阅读(174) 评论(0) 推荐(0)
摘要: 前言 在某些情况下,您可能需要将数据类型(如Pydantic模型)转换为与JSON兼容的数据类型(如dict、list等)。 比如,如果您需要将其存储在数据库中。 对于这种要求, FastAPI提供了jsonable_encoder()函数。 使用jsonable_encoder jsonable_ 阅读全文
posted @ 2023-09-17 10:53 上海-悠悠 阅读(346) 评论(0) 推荐(0)
摘要: 当你创建一个 FastAPI 路径操作 时,你可以正常返回以下任意一种数据:dict,list,Pydantic 模型,数据库模型等等。 FastAPI 默认会使用 jsonable_encoder 将这些类型的返回值转换成 JSON 格式, 默认情况下会以content-type: applica 阅读全文
posted @ 2023-09-16 09:42 上海-悠悠 阅读(1083) 评论(0) 推荐(0)
摘要: 前言 你可以使用定义 Query, Path 和 Cookie 参数一样的方法定义 Header 参数。 声明 Header 参数 首先导入 Header: from fastapi import FastAPI, Header 然后使用和Path, Query and Cookie 一样的结构定义 阅读全文
posted @ 2023-09-13 20:43 上海-悠悠 阅读(387) 评论(0) 推荐(0)
摘要: 前言 你可以像定义 Query 参数和 Path 参数一样来定义 Cookie 参数。 声明 Cookie 参数 首先,导入 Cookie: from fastapi import Cookie, FastAPI 声明 Cookie 参数的结构与声明 Query 参数和 Path 参数时相同。 第一 阅读全文
posted @ 2023-09-13 20:42 上海-悠悠 阅读(117) 评论(0) 推荐(0)
上一页 1 ··· 3 4 5 6 7 8 9 10 11 ··· 140 下一页