在 Axios 中的网址中更正数组



所以我最近开始使用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
},
}

我怎样才能像这样更正我的 URL。我不知道在哪里修复 axios 中的 URL。谢谢!

在调用axios之前,使用qs包将请求字符串化

const qs = require('qs')
const params = {
...other,
health:  ["alcohol-free", "celery-free"],
nutrient: {
CA: "100-200",
FAT: "100" // under 100mg
},
}
axios(qs.stringify(params))

最新更新