我在使用axios向这个API发送请求时遇到了很多困难,该API以对象的方式接受参数:
GET https://api.osrsbox.com/items?where={ "name": "Abyssal whip", "duplicate": false }
主要问题是axios会自动对字符串进行编码。在StackOverflow中寻找解决方案时,我谈到了以下几点:
const item = "Abyssal Whip"
const config = {
paramsSerializer: (params) => {
return querystring.stringify(params, { arrayFormat: 'brackets' });
},
params: {
where : {
name: item,
duplicate: false
}
}
}
axios.get("https://api.osrsbox.com/items", config).then( (resp) => {
[...]
})这导致以下请求:
https://api.osrsbox.com/items?where=%7B%22name%22:%22Abyssal%20Whip%22,%22duplicate%22:false%7D
我想知道我做错了什么。提前谢谢。
PS:我是从Node.js环境/服务器发出请求的。
发布于 2020-10-08 20:11:28
这是一个非常奇怪的格式,您的API接受。但我想这就是你要做的.
在我看来,通过将代码的相关部分更改为:
where: JSON.stringify({
name: item,
duplicate: false
}) (我希望你的示例请求中的空格是不需要的,如果它们是的话,这就不起作用了)
发布于 2020-10-08 20:14:01
您必须使用npm软件包"qs“。
)的对象
const requestBody ={
.;
}
console.log(err)) Axios.get("https://api.osrsbox.com/items",qs.stringyfy(requestBody),config) .then(res => console.log(res) .catch(err => .catch)“)
https://stackoverflow.com/questions/64269861
复制相似问题