问题描述
我在一个宠物项目中使用 axios,并有来自 BambooHR API (https://documentation.bamboohr.com/reference#request-custom-report-1) 的代码示例。他们使用 node-fetch,所以我将它重写为 axios。示例代码为:
const fetch = require("node-fetch");
const url =
"https://api.bamboohr.com/api/gateway.PHP/companyDomain/v1/reports/custom";
const options = {
method: "POST",qs: { format: "JSON" },headers: { "Content-Type": "application/json" },body: JSON.stringify({
fields: ["firstName","lastName"],title: "AllOfThem",}),};
fetch(url,options)
.then((res) => res.json())
.then((json) => console.log(json))
.catch((err) => console.error("error:" + err));
我将其翻译成 AXIOS 帖子:
let res = await axios.post(
"https://api.bamboohr.com/api/gateway.PHP/" + domain + "/v1/reports/custom",{
title: "AllOfThem",fields: ["firstName",},{
auth: {
username: seckey,password: "x",params: { format: "JSON" },headers: { Accept: "application/json" },}
);
但我收到 400 '错误请求'。我做错了什么?
解决方法
我遇到了类似的问题。 axios 存在一个服务器端请求伪造漏洞,尚未修复。这可能就是你得到 404 的原因。我正在尝试使用 fetch 而不是 axios 重写以下内容:
componentDidMount() {
axios
.get("http://localhost:3000/record")
.then((response) => {
this.setState({ records: response.data });
})
.catch(function (error) {
console.log(error);
});
}
也许我们可以就此合作?