<p dir="auto">Similar to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="154130084" data-permission-text="Title is private" data-url="https://github.com/axios/axios/issues/318" data-hovercard-type="issue" data-hovercard-url="/axios/axios/issues/318/hovercard" href="https://github.com/axios/axios/issues/318">#318</a> , I am unable to make a post request with axios on node. But on browser the same piece of code seems to work fine.</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="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 =&gt; {
  if (response.status === 200) {
    console.log(response.data);
    console.log('Removed host successfully');
  }
  return null;
}).catch(er =&gt; console.log(er));"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-s1">fdata</span> <span class="pl-c1">=</span> <span class="pl-k">new</span> <span class="pl-v">FormData</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-s1">fdata</span><span class="pl-kos">.</span><span class="pl-en">append</span><span class="pl-kos">(</span><span class="pl-s">'user'</span><span class="pl-kos">,</span> <span class="pl-s1">u</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-s1">fdata</span><span class="pl-kos">.</span><span class="pl-en">append</span><span class="pl-kos">(</span><span class="pl-s">'hostnames'</span><span class="pl-kos">,</span> <span class="pl-s1">n</span><span class="pl-kos">.</span><span class="pl-en">join</span><span class="pl-kos">(</span><span class="pl-s">' '</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">const</span> <span class="pl-s1">host</span> <span class="pl-c1">=</span> <span class="pl-s1">localStorage</span><span class="pl-kos">.</span><span class="pl-en">getItem</span><span class="pl-kos">(</span><span class="pl-s">'host'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">const</span> <span class="pl-s1">port</span> <span class="pl-c1">=</span> <span class="pl-s1">localStorage</span><span class="pl-kos">.</span><span class="pl-en">getItem</span><span class="pl-kos">(</span><span class="pl-s">'port'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-en">axios</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
  <span class="pl-c1">url</span>: <span class="pl-s">`http://<span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">host</span><span class="pl-kos">}</span></span>:<span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">port</span><span class="pl-kos">}</span></span>/hosts/remove`</span><span class="pl-kos">,</span>
  <span class="pl-c1">method</span>: <span class="pl-s">'post'</span><span class="pl-kos">,</span>
  <span class="pl-c1">data</span>: <span class="pl-s1">fdata</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">then</span><span class="pl-kos">(</span><span class="pl-s1">response</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">response</span><span class="pl-kos">.</span><span class="pl-c1">status</span> <span class="pl-c1">===</span> <span class="pl-c1">200</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s1">response</span><span class="pl-kos">.</span><span class="pl-c1">data</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">'Removed host successfully'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span>
  <span class="pl-k">return</span> <span class="pl-c1">null</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">catch</span><span class="pl-kos">(</span><span class="pl-s1">er</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s1">er</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">With unirest it works:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="unirest.post(`http://${host}:${port}/hosts/remove`)
.headers({ 'Content-Type': 'multipart/form-data' })
.field('user', u)
.field('hostnames', h.join(' '))
.end(response =&gt; {
  console.log(response.body);
});"><pre class="notranslate"><span class="pl-s1">unirest</span><span class="pl-kos">.</span><span class="pl-en">post</span><span class="pl-kos">(</span><span class="pl-s">`http://<span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">host</span><span class="pl-kos">}</span></span>:<span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">port</span><span class="pl-kos">}</span></span>/hosts/remove`</span><span class="pl-kos">)</span>
<span class="pl-kos">.</span><span class="pl-en">headers</span><span class="pl-kos">(</span><span class="pl-kos">{</span> <span class="pl-s">'Content-Type'</span>: <span class="pl-s">'multipart/form-data'</span> <span class="pl-kos">}</span><span class="pl-kos">)</span>
<span class="pl-kos">.</span><span class="pl-en">field</span><span class="pl-kos">(</span><span class="pl-s">'user'</span><span class="pl-kos">,</span> <span class="pl-s1">u</span><span class="pl-kos">)</span>
<span class="pl-kos">.</span><span class="pl-en">field</span><span class="pl-kos">(</span><span class="pl-s">'hostnames'</span><span class="pl-kos">,</span> <span class="pl-s1">h</span><span class="pl-kos">.</span><span class="pl-en">join</span><span class="pl-kos">(</span><span class="pl-s">' '</span><span class="pl-kos">)</span><span class="pl-kos">)</span>
<span class="pl-kos">.</span><span class="pl-en">end</span><span class="pl-kos">(</span><span class="pl-s1">response</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s1">response</span><span class="pl-kos">.</span><span class="pl-c1">body</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<ul dir="auto">
<li>axios version: <em>v0.16.2</em></li>
<li>Environment: <em>node v8.0.0, windows 8.1</em></li>
</ul>
<p dir="auto">From the documentation of axios it seems that nodejs doesn't natively support FormData objects</p>
<p dir="auto"><code class="notranslate"> // data is the data to be sent as the request body // Only applicable for request methods 'PUT', 'POST', and 'PATCH' // When no transformRequest is set, must be of one of the following types: // - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams // - Browser only: FormData, File, Blob // - Node only: Stream data: { firstName: 'Fred' },</code></p>
<p dir="auto">So if I want to use axios in a jasmine test running with nodejs, then how would I go ahead and post a multipart/formdata object to a website?</p>
<p dir="auto">Thanks for your help..</p>