微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

更正 AXIOS 版本的 node-fetch POST

如何解决更正 AXIOS 版本的 node-fetch POST

我在一个宠物项目中使用 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);
      });
  }

也许我们可以就此合作?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。