You need to enable JavaScript to run this app. 如何在Postman中测试携带多个参数的Nest.js GET请求
优惠活动
大模型
产品
解决方案
定价
更多

如何在Postman中测试携带多个参数的Nest.js GET请求

问题根因
  • 装饰器使用错误:你当前用的@Param装饰器是用于提取路由路径占位参数的,比如接口路径定义为@Get(':sport/:gender')时才能用@Param拿到对应值,而你Postman里的参数是放在URL问号后的查询参数,需要用@Query装饰器提取。
  • 参数名不匹配:你请求里传递的查询参数key为sportId,但控制器中@Param绑定的key是sport,二者名称不一致无法正常赋值。

方案1:保留现有Postman请求格式(推荐,符合过滤类接口的常用规范)

修改控制器代码,把@Param换成@Query,对齐参数名即可:

//controller
import { Query } from '@nestjs/common'; // 先引入Query装饰器

@Get()
getSelectorsBySportGender(@Query('sportId') sportId: string, @Query('gender')gender: string)
{
    return this.selectorsService.findSelectorsBySportGender(sportId, gender);
}

修改完成后你原来的Postman请求http://localhost:3000/selectors?sportId=5f43c1ee-6be3-4881-913c-0aa89be49ae2&gender=female就可以正常返回结果。


方案2:改成路由参数传递方式

如果要继续用@Param,需要修改路由定义和请求路径:

//controller
@Get(':sportId/:gender') // 新增路由占位参数
getSelectorsBySportGender(@Param('sportId') sportId: string, @Param('gender')gender: string)
{
    return this.selectorsService.findSelectorsBySportGender(sportId, gender);
}

对应的Postman请求路径改为:http://localhost:3000/selectors/5f43c1ee-6be3-4881-913c-0aa89be49ae2/female即可。


内容的提问来源于stack exchange,提问作者Goga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 11:24:04