【大模型应用开发-FastAPI框架】(三)FastAPI路由参数(1)

一、FastAPI的安装

参考

【大模型应用开发-FastAPI框架】(一)FastAPI概述和安装-优快云博客

二、路径操作装饰器中的路径参数

1、声明路径参数


使用Python格式字符串的语法声明路径参数,例子

from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}") 
def read_item(item_id):
    return {"item_id": item_id}



上述代码运行之后,路径参数 item_id 的值会作为read_item函数参数 item_id 的值。

因此,如果你运行上述示例,然后跳转到 http://127.0.0.1:8000/items/foo, 你将会看见这样的回应:

{"item_id":"foo"}



2、声明路径参数的类型


使用 标准的Python类型注释在函数中声明路径参数的类型,例子1:

from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}



上述将参数item_id的类型定义为int类型

例子2:

from fastapi import FastAPI

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

forest_long

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值