知识卡片
装饰器绑定请求路由
FastAPI 的 HTTP 方法装饰器把路径与处理函数绑定,例如 @app.get('/orders') 注册读取端点。处理函数返回的数据由框架转换成 HTTP 响应;需要不同结果码时可在装饰器中设置状态码。路由声明同时承载了“请求到哪里”和“由谁处理”。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
原书出处
参考来源
- 位置:《Microservice APIs》第 2 章《A basic API implementation》(源文件:_epub-src/OEBPS/Text/02.htm)
- 结论依据:2.3 节说明 FastAPI 的 get/post 装饰器注册端点、接收 URL 路径,并将函数结果转换为 HTTP 响应;状态码可通过装饰器参数覆盖默认值。
- 原始内容:FastAPI exposes decorators named after HTTP methods, such as get() and post() . We use these decorators to register our API endpoints. FastAPI’s decorators take at least one argument, which is the URL path we want to register. ... FastAPI responses include a 200 (OK) status code by default, but we can change this behavior by setting the status_code parameter in the endpoints’ decorators.