Nodejs cross-fetch, post file from google bucket

Viewed 16

im trying to post a csv file to other server from my backend. to create my file and upload it to storage i use fast-csv and @google-cloud/storage:

const data = [
        {
            name: 'John',
            surname: 'Snow',
            age: 26,
            gender: 'M'
        }, {
            name: 'Clair',
            surname: 'White',
            age: 33,
            gender: 'F',
        }
];
const blob = storage.bucket("myBucket").file("myfile.csv");
const blobStream = blob.createWriteStream({
    resumable: false,
});
fastcsv.write(data, { headers: true }).pipe(blobStream);

i use form-data and raw-body to append the file buffer to form

const formData = require("form-data");
const getRawBody = require('raw-body')
const form = new formData();
const buffer = await getRawBody(blob.createReadStream())
form.append('file', buffer);

and finally to post

 try {
     const url = 'https://webhook.site/004da......';
     const response = await fetch(url, {
                method: "post",
                body: form,
                headers: {
                    "Content-Type": "multipart/form-data"
                }
            });

      } catch ... 

what i get is this :

enter image description here

but what i need is to send it as file not as Raw Content like this: enter image description here

and here is how the working post looks like in postman: enter image description here any idea how to replicate the post from postman ? when i try to post the blob instead of formData i get:

enter image description here

0 Answers
Related