Axios vs request - preflight status code error in only Axios
See original GitHub issueThe 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:
- Created 4 years ago
- Reactions:5
- Comments:6
Top GitHub Comments
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.
Closed due to stale. I wrote a note about how to check the adapter. Feel free to remind me if you have further discoveries.