从邮递员将数组传递到 FastAPI GET 端点

问题描述 投票:0回答:1

我在 FastAPI 中有以下端点:

@router.get("/", response_model=ApiResponseMultiple[MyObject], response_model_exclude_none=True)
async def get_objs(
    request: Request,
    ids: list
):
    """Get stuffs"""
    print(ids)
...

我无法获取要填充的列表。邮递员打电话。我尝试从邮递员那里传递以下信息:

  • 1,2
  • [1,2]
  • ids=1&ids=2
  • ids[]=1&ids[]=2
  • ids[0]=1&ids[1]=2

总是我的

print
声明吐出
None

list get postman fastapi
1个回答
0
投票

即使您将参数

ids
作为列表发送,FastAPI 也期望它作为主体参数,并且不允许为 GET 请求发送主体数据:)

修改代码以将参数

ids
显式标记为查询参数。

这是一个示例:

from fastapi import Query

@router.get("/")
async def get_objs(
    ids: list = Query(...)
):
    """Get stuffs"""
    print(ids)
...
© www.soinside.com 2019 - 2024. All rights reserved.