【发布时间】:2021-05-24 23:50:23
【问题描述】:
我在一个宠物项目中使用 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", "lastName"],
},
{
auth: {
username: seckey,
password: "x",
},
params: { format: "JSON" },
headers: { Accept: "application/json" },
}
);
但我收到 400 条“错误请求”。我哪里错了?
【问题讨论】:
-
据我所知,您所做的一切都是正确的。他们的 API 示例代码虽然不是很好。在documentation 中看不到
node-fetch的qs选项。你的 Axios 实现看起来不错
标签: javascript node.js axios node-fetch