title: how to add file with formData in axios request using node.js
body: <p>I'm currently building my first npm package which contain axios requests.
  There is something wrong when i want to add a file as a parameter in a request and i don't know why.</p>
  <p>I'm using form-data library and fs to add my local file.</p>
  <p>First i created an abstract class to add my token in a header for every requests and a post method that will be used by my other classes :</p>
  <pre><code>import axios from 'axios'
  import url from '../utils/constants.js'
  
  class ApiBase {
    constructor(apiKey) {
      if (this.constructor === ApiBase) {
        throw new TypeError('Abstract class &quot;ApiBase&quot; cannot be instantiated directly');
      };
  
      let service = axios.create({
        headers: {
          'Authorization' : `Bearer ${apiKey}`
        },
      })
      
      this.service = service
    }
  
    post(payload, path) {
      return this.service.request({
        method: 'POST',
        url: url + path,
        data: payload
      })
    }
  }
  export default ApiBase
  
  </code></pre>
  <p>My class which extends ApiBase :</p>
  <pre><code>import ApiBase from &quot;./api_base.js&quot;;
  import { default as FormData } from &quot;form-data&quot;
  
  class MyClass extends ApiBase {
    constructor(apiKey) {
      super(apiKey)
    }
  
    formatData (files, providers, language) {
      const user = new FormData();
        const key = [
          &quot;files&quot;,
          &quot;providers&quot;,
          &quot;language&quot;,
        ];
        const value = [
          files,
          JSON.stringify(providers),
          language
        ];
        for (let i = 0; i &lt; value.length; i++) {
          user.append(key[i], value[i]);
        }
      return user  
    }
  
    myFeature = (files, providers, language) =&gt; {
      const userData = this.formatData(files, providers, language);
      return this.post(userData, 'endpointApi')
    }
  
  }
  
  export default MyClass
  </code></pre>
  <p>Then i call myFeature in index.js to test it and try to console.log the api response :</p>
  <pre><code>import MyClass from './api/myclass.js'
  import * as fs from 'fs';
  
  const testFeature = new MyClass('MYAPIKEY')
  var myFile = fs.createReadStream('./testData/Doc.jpg');
  testFeature.myFeature(myFile,['microsoft'], &quot;fr-FR&quot;).then((response) =&gt; {
    console.log(response.data)
  })
  .catch((error) =&gt; {
    console.log(error.response.data);
  });
  </code></pre>
  <p>When i send request with other features that not need files it works...</p>
  <p>Maybe something went wrong with formData or something missing in the headers</p>