Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
Axios not sending headers, request failing, getting 401 error

I've been fetching some data from a private API with axios, but now I'm having a problem fetching data from one specific endpoint.

The interesting thing is that with the built in fetch API, I'm receiving 200 response, but the identical request with axios keeps retuning 401 error. Any idea what can be the problem?

This code works:

const upVoteCommentTwo = async () => {
  console.log(localStorage.getItem("access_token"));
  try {
    const response = await fetch(
      `https://exammple.com/comments/${commentId}/vote/up`,
      {
        method: "POST",
        headers: {
          "X-API-KEY": "XXX",
          "Authorization": localStorage.getItem("access_token"),
        },
      }
    );

    console.log(await response.status);
  } catch (err) {
    console.log(err);
  }
};

And this does not work:

const upVoteCommentOne = async () => {
  console.log(localStorage.getItem("access_token"));
  try {
    const response = await axios.post(
      `https://example.com/comments/${commentId}/vote/up/`,
      {
        headers: {
          "Content-Type": "application/json",
          "X-API-KEY": "XXX",
          "Authorization": localStorage.getItem("access_token"),
        },
      }
    );

    console.log(response.status);
  } catch (err) {
    console.log(err);
  }
};

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The problem is coming from axios's parameters. One valid way according to the doc and that seems to be working for most people according to this issue on GitHub is to call it like this:

const response = await axios({
  method: "post",
  url: `https://example.com/comments/${commentId}/vote/up/`,
  headers: {
    "Content-Type": "application/json",
    "X-API-KEY": "XXX",
    "Authorization": localStorage.getItem("access_token"),
  },
  data: {},
});

about 4 years ago · Juan Pablo Isaza Report

0

const upVoteCommentOne = async () => {
  console.log(localStorage.getItem("access_token"));
  try {
    const response = await axios.post(
          `https://example.com/comments/${commentId}/vote/up/`,
         {
           //body:...if any
         }
        headers: {
          "X-API-KEY": "XXX",
          "Authorization": localStorage.getItem("access_token"),
        },
      }
    );

    console.log(response.status);
  } catch (err) {
    console.log(err);
  }
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!