GET file from an url and forward it to external file upload API endpoint as multipart form data in NodeJS

Viewed 243

I would like to implement an endpoint that has 3 inputs: newFileName, mimeType and url. This endpoint should request from the "url" and without filling the memory it should stream the data to an external API's file upload endpoint. The external API accepts formdata.

I have tried to forward the stream as FormData but I could not manage to connect (pipe?) the parts.

const FormData = require('form-data');
const http = require('http');
//...
    const form = new FormData();
    http.request(url, (response) => {
        form.append('attachment', response, {
            filename: newFileName,
            contentType: mimeType
        });
    });
    http.request({
        method: 'POST',
        url: attachmentUrl.href,
        ignoreError: true,
        rejectUnauthorized: false,
        headers: form.getHeaders(),
        body: form.getBuffer()
    });

Could someone please help me to connect the two request in a data stream where the memory only store the chunks and not the whole thing, meanwhile the external API recives multipart form data?

Thank you for your time and answers!

0 Answers
Related