Authenticated POST request returning 403 (axios)

System Information
  • Strapi Version: v3.5.4 Community
  • Operating System: Windows 10
  • Database: SQLite
  • Node Version: v14.15.4
  • NPM Version: v6.14.10
  • Yarn Version: v1.22.10

I am trying to use axios to make an authenticated POST request to a collection.

const {data} = await axios
  .post('http://localhost:1337/crypto-accounts',  {
    data: {
      username: `${user}`,
      password: `${pass}`,
    },
    headers: {
      Authorization: `Bearer ${response.data.jwt}`
    }
  });
console.log(data);

Error:

UnhandledPromiseRejectionWarning: Error: Request failed with status code 403

response.data.jwt is a working bearer token as it works with authenticated GET requests just fine. Any ideas on this problem?

As we talked in the thread, this might be an issue while using axios.post method.

According to docs axios.post(url[, data[, config]]) has 3 parameters, you are only sending 2.

The possible possible solution might be:

const {data} = await axios
  .post('http://localhost:1337/crypto-accounts',  {
      username: `${user}`,
      password: `${pass}`,
    },
   {
    headers: {
      Authorization: `Bearer ${response.data.jwt}`
    }
  });
console.log(data);
2 Likes

This worked! Thanks a bunch.