【问题标题】:Correct AXIOS version of node-fetch POST正确的节点获取 POST 的 AXIOS 版本
【发布时间】: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-fetchqs 选项。你的 Axios 实现看起来不错

标签: javascript node.js axios node-fetch


【解决方案1】:

我遇到了类似的问题。 axios 存在服务器端请求伪造漏洞,尚未修复。这可能就是你得到 404 的原因。我正在尝试使用 fetch 而不是 axios 重写以下内容:

  componentDidMount() {
    axios
      .get("http://localhost:3000/record")
      .then((response) => {
        this.setState({ records: response.data });
      })
      .catch(function (error) {
        console.log(error);
      });
  }

也许我们可以在这方面进行合作?

【讨论】:

    猜你喜欢
    • 2016-10-15
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多