title: Convert file binary to FormData server-side using NodeJS
body: <p>I'm building a web application that requires to me to receive file binaries on a server-side endpoint, convert those binaries to FormData, then POST them to another endpoint. The second endpoint only accepts &quot;multipart/form-data&quot;.</p>
  <p>I'm using axios to create the requests, and NodeJS on the server.</p>
  <p>The files are posted from the client as form data, but I'm unable to manipulate them as such once they reach the server.</p>
  <p><strong>How do I compel file binaries to FormData in my server-side scripts?</strong></p>
  <p>Client:</p>
  <pre class="lang-js prettyprint-override"><code>const handleImageSubmit = async (attachments) =&gt; {
      try {
        let image
        if (attachments.length) {
          const formData = new FormData()
  
          for (let i = 0; i &lt; attachments.length; i += 1) {
            formData.append(
              `files.${attachments[i].name}`,
              attachments[i],
              attachments[i].name
            )
          }
  
          const config = {
            headers: { 'Content-Type': 'multipart/form-data' },
          }
  
          image = await axios.post(
            '/api/proxy/upload',
            formData,
            config
          )
        }
        console.log('Files dispatched to server')
      } catch (err) {
        console.error(err)
      }
    }
  </code></pre>
  <p>Server-side:</p>
  <pre class="lang-js prettyprint-override"><code>const upload = async (req, res) =&gt; {
    try {
      const data = new FormData()
  
     // Convert the binaries in req.body into FormData
     const myFiles = ...
  
      data.append('files',
        myFiles)
  
      const config = {
        method: 'post',
        url: `${process.env.API_URL}/upload`,
        headers: {
          Authorization: `Bearer ${req.headers['auth-token']}`,
          ...data.getHeaders()
        },
        data: data,
      }
  
      const signal = await axios(config)
      res.json(signal.data)
  
    } catch (err) {
      console.error(err)
      res.status(fetchResponse?.status || 500).json(err)
    }
  }
  </code></pre>