title: how to upload file saved in memory by multer to another API
body: <p>I have <strong>2 separate NodeJS APIs</strong> that uses <a href="https://github.com/expressjs/multer" rel="noreferrer">multer</a> to save a file in <strong>memory</strong>. </p>
  
  <p>My express middleware looks like this</p>
  
  <pre><code>import multer from 'multer';
  const storage = multer.memoryStorage();
  
  export default multer({ storage }).single('image');
  </code></pre>
  
  <p>I am able to receive the file which is saved in memory successfully so my <strong>req.file.image</strong> looks like this</p>
  
  <pre><code>{ 
    fieldname: 'image',
    originalname: 'image-2017-08-28-13-47-31-218.png',
    encoding: '7bit',   mimetype: 'image/png',
    buffer: &lt;Buffer 89 50 4e 47 0d 0a 1a 0 ... &gt;, 
    size: 493181
  }
  </code></pre>
  
  <p>After receiving the file, on the <strong>first</strong> API, I need to send it to the <strong>second API</strong> that also uses <strong>multer</strong> &amp; <strong>express</strong></p>
  
  <pre><code>function secondApiReceiveImage(req, res) {
    console.log(req.file)
    console.log(req.files)
    console.log(req.body)
    res.send('ok');
  }
  </code></pre>
  
  <p>I tried sending using the following implementations</p>
  
  <p>Via <a href="https://github.com/request/request#multipartform-data-multipart-form-uploads" rel="noreferrer">https://github.com/request/request#multipartform-data-multipart-form-uploads</a></p>
  
  <pre><code>import request from 'request';
  
  function firstApiReceiveImage(req, res) {
    const options = {
      url:'http://SECOND_API/api/image',
      formData: { image: req.file.buffer }
    };
    request.post(options, (err, httpResponse, body) =&gt; {
      console.log('err', err);
      console.log('body', body);
    });
  }
  </code></pre>
  
  <p>In this case, logs of <strong>req.file</strong>, <strong>req.files</strong> and <strong>req.body</strong> are all <strong><em>undefined</em></strong> on <strong>secondApiReceiveImage</strong> API handler function </p>
  
  <p>My next try was with <a href="https://github.com/form-data/form-data" rel="noreferrer">https://github.com/form-data/form-data</a></p>
  
  <pre><code>import multer from 'multer';
  const storage = multer.memoryStorage();
  
  export default multer({ storage }).single('image');
  
  function firstApiReceiveImage(req, res) {
    const CRLF = '\r\n';
    const form = new FormData();
    const opts = {
      header: `${CRLF} + '--' + ${form.getBoundary()} + ${CRLF} + 'X-Custom-Header: 123' + ${CRLF} + ${CRLF}`,
      knownLength: 1
    };
  
    form.append('image', req.file.image.buffer, opts);
    form.submit('http://SECOND_API/api/image', (err, res) =&gt; {
      console.log('err', err);
      console.log('res', res);
    });
  }
  </code></pre>
  
  <p>I got the same result, <strong>undefined</strong> for <strong>req.file</strong>, <strong>req.files</strong> &amp; <strong>req.body</strong> on the second API</p>
  
  <p>These are my middlewares for both APIs aside from multer BTW</p>
  
  <pre><code>app.use(compression());
  app.use(helmet());
  app.use(cors());
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  </code></pre>
  
  <p>I could have had an easier life if I can persist the file on the <strong>first API</strong>, but we are not allowed to save to disk in this case :(</p>
  
  <p>Any advice for me?</p>