Axios中URL中的正确数组

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

因此,我最近开始使用Axios创建我的第一个项目。当我尝试从API提取数据时,默认情况下(我认为),axios给我提供如下URL:

 /search?health[]=alcohol-free&health[]=celery-free&nutrient[]=calcium&nutrient[]=carbs

但是API(Edamam API)需要:

/search?health=alcohol-free&health=celery-free&nutrients%5BCA%5D=100-&nutrients%5BFAT%5D=100

我的参数:

const params = {
    ...other,
    health:  ["alcohol-free", "celery-free"],
    nutrient: {
       CA: "100-200",
       FAT: "100" // under 100mg
    },
}

我该如何更正我的网址。我不知道在axios中修复URL的位置。谢谢!

axios
1个回答
0
投票

在调用qs之前,使用axios包对您的请求进行字符串化处理>

const qs = require('qs')

const params = {
    ...other,
    health:  ["alcohol-free", "celery-free"],
    nutrient: {
       CA: "100-200",
       FAT: "100" // under 100mg
    },
}

axios(qs.stringify(params))
© www.soinside.com 2019 - 2024. All rights reserved.