POST request works in Browser but not on Node
See original GitHub issueSimilar to #318 , I am unable to make a post request with axios on node. But on browser the same piece of code seems to work fine.
const fdata = new FormData();
fdata.append('user', u);
fdata.append('hostnames', n.join(' '));
const host = localStorage.getItem('host');
const port = localStorage.getItem('port');
axios({
url: `http://${host}:${port}/hosts/remove`,
method: 'post',
data: fdata
}).then(response => {
if (response.status === 200) {
console.log(response.data);
console.log('Removed host successfully');
}
return null;
}).catch(er => console.log(er));
With unirest it works:
unirest.post(`http://${host}:${port}/hosts/remove`)
.headers({ 'Content-Type': 'multipart/form-data' })
.field('user', u)
.field('hostnames', h.join(' '))
.end(response => {
console.log(response.body);
});
- axios version: v0.16.2
- Environment: node v8.0.0, windows 8.1
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:26
Top Results From Across the Web
API works in browser but not when pulled from Node
First, I would send Accept: application/json HTTP header with your node request. See if that triggers a JSON response. Then I would debug...
Read more >API GET works with browser, but not with axios.get (CORS)
I am trying to use some public API and it works with the browser with GET request, but when I do get request...
Read more >Node.js Error: Cannot GET/ from running the url on the web ...
Reason: Since in the server file, we create a get route for '/messages' URL but inside the browser, we try to get the...
Read more >HTTP Methods GET vs POST - W3Schools
The POST Method · POST requests are never cached · POST requests do not remain in the browser history · POST requests cannot...
Read more >Cross-Origin Resource Sharing (CORS) - MDN Web Docs
For security reasons, browsers restrict cross-origin HTTP requests initiated from scripts. For example, XMLHttpRequest and the Fetch API follow ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This might be considered a duplicate of #789.
I was able to use the
form-data
package with Axios in nodejs. It basically provides aFormData
-like interface. You have to be careful to pass the headers it generates to Axios manually, however. For example:For nodejs users solve by using the querystring lib, as follows: