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

576
Views
axios returns 404 for a post request with form data

I am trying to get a graph token from Azure AD with a post request having form data through Axios. The response throws an error Request failed with status code 404. The below is the code,

const axios = require('axios')
const FormData = require('form-data')
    
const bodyFormData = new FormData()
    
bodyFormData.append('client_id', <client id>)
bodyFormData.append('client_secret', <secret>)
bodyFormData.append('scope', <scope>)
bodyFormData.append('requested_token_use', <token use>)
bodyFormData.append('assertion', <token>)
    
axios
.post('https://login.microsoftonline.com/<tenantId>/oauth2/v2.0/token', bodyFormData, {
headers: bodyFormData.getHeaders(),
})
.then((response) => {
console.log('AXIOS RESPONSE ', response)
})
.catch((err) => {
console.log('AXIOS ERROR ', err)
})

The post request works fine in the postman. The response in Axios is also as expected if bodyFormData is removed from Axios request. When bodyFormData is added to the request I encounter an error.

AXIOS ERROR Error: Request failed with status code 404

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Instead of using form-data use querystring

const axios = require('axios')
const querystring = require('querystring');

const data = querystring.stringify({
    client_id: '<client id>',
    client_secret: '<secret>',
    scope: '<scope>',
    requested_token_use: '<token use>',
    assertion: '<token>'
});

axios.post('https://login.microsoftonline.com/<tenantId>/oauth2/v2.0/token', data)
    .then((response) => {
        console.log('AXIOS RESPONSE ', response)
    })
    .catch((err) => {
        console.log('AXIOS ERROR ', err)
    });
over 4 years ago · Santiago Trujillo Report

0

Try removing the headers part, it should work.

There should not be any reason having to get the headers from the FormData object, as it’s an API and will use other forms of authentication and could mess up the call.

It’s not a data scraping call that might be blocked by some security measure.

over 4 years ago · Santiago Trujillo 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!