question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Axios vs request - preflight status code error in only Axios

See original GitHub issue

The issue/Question When sending a post request with axios in a Nodejs environment I get the error: ‘Response for preflight has invalid HTTP status code 401’

This seems to be related to CORS and the OPTIONS preflight request. I noticed Postman didn’t send this request (because it isn’t a browser and doesn’t care about CORS) and as such the request was successful. I grabbed the code snippet postman gives for NodeJs which uses the request npm module. I then ran it using Node and it worked without a problem.

Is there a way to disable this preflight request since I am running axios in a Node environment? I would also really appreciate if someone happened to know what the actual difference is in the libraries that is causing one to work while the other doesn’t.

Example Code The axios version:

var bodyFormData = new FormData();
var data = { x: 1, y: 'D' };
for (var k in data) {
    bodyFormData.append(k, data[k]);
}

axios({
    method: "POST",
    url: url,
    baseURL: this.baseURL,
    data: bodyFormData,
    headers: {
        ...bodyFormData.getHeaders(),
        Authorization: `Bearer ${bearerToken}`
    },
});

The request version:

var data = { x: 1, y: 'D' };

var options = {
    method: "POST",
    url,
    headers: {
        "cache-control": "no-cache",
        Authorization: `Bearer ${bearerToken}`,
        "Content-Type": "application/x-www-form-urlencoded"
    },
    form: data
};

request(options);

Let me know if there is any other information needed, and thanks for your time in advance.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:6

github_iconTop GitHub Comments

4reactions
tylerzeycommented, Apr 23, 2019

This also is an issue for me. I’m consuming an external vendor api from nodejs and axios seems to be sending a preflight options call as if I was using it in the browser.

Going to use request or got now instead of axios.

1reaction
chinesedfancommented, Mar 8, 2020

Closed due to stale. I wrote a note about how to check the adapter. Feel free to remind me if you have further discoveries.

Read more comments on GitHub >

github_iconTop Results From Across the Web

axios and reactjs: Response for preflight has invalid HTTP ...
I have the same issue only with axios POST request. I get "pre-flight response has invalid http status code". I have added the...
Read more >
Axios Network Error when making HTTP request [Solved]
To solve the "Axios Network Error", make sure your server sends back all the necessary CORS headers to enable requests from a different...
Read more >
axios has been blocked by cors policy - You.com
What “preflight” means: before the browser tries the POST in the code in the question, it first sends an OPTIONS request to the...
Read more >
Request Config | Axios Docs
These are the available config options for making requests. Only the url is required. Requests will default to GET if method is not...
Read more >
Request to api from axios (cross domain) (CORS) error
or, it may be some other config params so just do a search. You can just use the request method which takes in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found