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.

POST request works in Browser but not on Node

See original GitHub issue

Similar 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:26

github_iconTop GitHub Comments

348reactions
binkicommented, Aug 4, 2017

This might be considered a duplicate of #789.

I was able to use the form-data package with Axios in nodejs. It basically provides a FormData-like interface. You have to be careful to pass the headers it generates to Axios manually, however. For example:

const axios = require('axios');
const FormData = require('form-data');

const form = new FormData();
// Second argument  can take Buffer or Stream (lazily read during the request) too.
// Third argument is filename if you want to simulate a file upload. Otherwise omit.
form.append('field', 'a,b,c', 'blah.csv');
axios.post('http://example.org/endpoint', form, {
  headers: form.getHeaders(),
}).then(result => {
  // Handle result…
  console.log(result.data);
});
22reactions
heldridacommented, May 22, 2018

For nodejs users solve by using the querystring lib, as follows:

const querystring = require('querystring')
axios
  .post(URL, querystring.stringify(data))
  .then((response) => ...)
  .catch((error) => ...)
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

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