在何时/何处使用FastAPI中的正文/路径/查询/字段? [英] When/Where to use Body/Path/Query/Field in FastAPI?

查看:107
本文介绍了在何时/何处使用FastAPI中的正文/路径/查询/字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注FastAPI的教程,到目前为止,我对何时/何地在FastAPI中使用Body/Path/Query/Field?,因为它们似乎都以相同的方式工作,所以本教程对它们的 distincction 使用了模糊的解释,还是我?缺少什么?

I'm following the tutorial from FastAPI, and so far I have doubts about When/Where to use Body/Path/Query/Field in FastAPI? because all of them seem to work the same way, the tutorial uses vague explanations about their distinction, or am I missing something?

奖励问题: * 真的有用吗?我已经在本教程的示例代码,但我看不出有什么区别.

Bonus question: Is * really useful? I've set/omitted it in the sample code of tutorial, but I don't see the difference.

推荐答案

实际上,它们是完全不同的.

Actually, they are completely different.

让我们以用FastAPI链接标记的问题的URL为例,并分为多个部分.

Let's take the URL of questions tagged with FastAPI link as an example and split into parts.

https://stackoverflow.com/questions/tagged/fastapi?sort=Newest&uqlId=26120

  • stackoverflow.com ->域
  • /问题->路径
  • /已标记->路径
  • /fastapi ->路径参数.
  • sort =最新->查询参数.
  • uqlId = 26120 ->查询参数.
    • stackoverflow.com -> domain
    • /questions -> Path
    • /tagged -> Path
    • /fastapi -> Path param.
    • sort=Newest -> Query Param.
    • uqlId=26120 -> Query Param.
    • 如果要在FastAPI中创建此文件,它将类似于以下内容.

      If you wanted to create this in FastAPI it would look something like this.

      from enum import Enum
      
      
      class SortTypes(str, Enum):
          newest: str = "Newest"
          unanswered: str = "Unanswered"
          active: str = "Active"
          bountied: str = "Bountied"
      
      
      @app.get("/questions/tagged/{tag}")
      async def get_questions_with_tags(tag: str, sort: SortTypes, uqlId: int):
          return ...
      

      查询参数和路径参数几乎相同.

      Query params and path params work nearly the same.

      但是身体完全不同.

      您无法从URL中看到请求的正文,您的客户端以字节为单位发送了HTTP正文,但是它可以包含任何内容,并且您需要通过HTTP标头指定要包含的正文.

      You can not see the body of the Request from URL, your client sent HTTP Body as bytes but it can contain anything, and you need to specify what body contains via HTTP Headers.

      这样做,您正在告诉服务器它应该如何处理该主体.例如

      By doing that, you are telling the server how it should handle that body. For example

      假设您正在发送JSON {"name":"foo"} ,它将与该标头 {"Content-Type":"application/json''} ,您的客户端会处理此问题,因为默认情况下,FastAPI返回 JSONResponse ,还有其他响应类型,例如 StreamingResponse FileResponse HTMLResponse 等(您可以从此处).

      Imagine you are sending a JSON {"name": "foo"} and it will be sent with this header {"Content-Type": "application/json"}, your client handles this because by default FastAPI returns JSONResponse,there are also other response types like StreamingResponse, FileResponse, HTMLResponse etc (You can read common Content-Type's from here).

      查询参数和路径参数之间的主要区别是可以从URL访问它们并且它们是字符串.但是Body通常是携带数据的.

      The main difference between Query params and the Path params is they are accessible from URL and they are strings. But Body is but and usually, it carries data.

      假设您有一个更大的应用程序,并且正在使用 Query 参数进行客户端和服务器之间的所有通信.这将是一场彻底的灾难.

      Imagine you have a kinda bigger application and you are using Query params for all your communication between the client and the server. It will be a complete disaster.

      例如,现在您正在阅读此答案,对吗?但是它是怎么来的呢?您的身分证件,您的声誉,问题本身的所有详细信息都会作为正文出现.想象一下,将所有这些发送到URL中,完全是灾难.

      For example, now you are reading this answer right? But how did it came? All the details your id, your reputation, the question itself comes as body. Imagine sending all this inside the URL, total disaster.

      要进一步阅读,您可以查看以下答案以了解设计REST API的最佳实践.

      For further reading, you can check these answers to see Best Practices for designing a REST API.

      这篇关于在何时/何处使用FastAPI中的正文/路径/查询/字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆