0

I use axios in a pet project and have a code sample from the BambooHR API (https://documentation.bamboohr.com/reference#request-custom-report-1). They use node-fetch, so I was rewriting it to axios. The sample code is:

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));

I translated it into a AXIOS post:

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" },
  }
);

But I get a 400 'Bad request'. What do I wrong?

dhruw lalan
  • 791
  • 1
  • 11
  • 23
Martin Schlott
  • 4,369
  • 3
  • 25
  • 49
  • 1
    As far as I can tell, you've done everything right. Their API example code isn't great though. Can't see a `qs` option for `node-fetch` in the [documentation](https://www.npmjs.com/package/node-fetch#options). Your Axios implementation seems fine – Phil Feb 23 '21 at 05:42

1 Answers1

0

I was having similar problems. There is a server side request forgery vulnerability with axios that has not been fixed. That may be why you're getting 404. I'm trying to re-write the following using fetch instead of axios:

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

Maybe we can collaborate on this?

codereyes
  • 47
  • 9