1

I am trying to create a pdf file on my google drive folder that I have downloaded through Gmail API, using a multipart/related request and Google Auth Library.

enter image description here

So this is the object the Gmail API is sending to me. I am able to create a pdf file out of it, using base64topdf.

My issue is to send this file to my google drive folder.

This is my base code :

const response = await auth.request({
    url: 'https://www.googleapis.com/upload/drive/v3/files',
    method: 'post',
    headers: {
        'Content-Type': `multipart/related; boundary=${boundary}`
    },
    params: {
        uploadType: 'multipart'
    },
    data
});

I have tried different ways to send my file through the data object with the help of this page, and by looking into the code of google's node API which do the following:

params.uploadType = 'multipart';
const multipart = [
    {'Content-Type': 'application/json', body: JSON.stringify(resource)}, {
        'Content-Type':
        media.mimeType || (resource && resource.mimeType) || defaultMime,
        body: media.body  // can be a readable stream or raw string!
    }
];
const boundary = uuid.v4();
const finale = `--${boundary}--`;
const rStream = new stream.PassThrough();
const isStream = isReadableStream(multipart[1].body);
headers['Content-Type'] = `multipart/related; boundary=${boundary}`;
for (const part of multipart) {
    const preamble =
        `--${boundary}\r\nContent-Type: ${part['Content-Type']}\r\n\r\n`;
    rStream.push(preamble);
    if (typeof part.body === 'string') {
        rStream.push(part.body);
        rStream.push('\r\n');
    } else {
        part.body.pipe(rStream, {end: false});
        part.body.on('end', () => {
            rStream.push('\r\n');
            rStream.push(finale);
            rStream.push(null);
        });
    }
}
if (!isStream) {
    rStream.push(finale);
    rStream.push(null);
}
options.data = rStream;

I've tried using Buffers, plain strings... I always end up having a Error: Malformed multipart body. or a PDF that only displays the data of the Gmail API object as a plain string.

Cannot find anything on how to do it with javascript and not using form-data! Can anyone help me out?

Devz
  • 563
  • 7
  • 23
  • have you tried this solution? https://stackoverflow.com/a/35182924/1841839 – Linda Lawton - DaImTo Feb 16 '18 at 09:25
  • @DalmTo I've just tried, my file is uploaded but it's just a pdf which content is the data of the Gmail API object as a plain string. I think this is the issue, I have to convert this data string into something else, but I don't figure out what. – Devz Feb 16 '18 at 10:23
  • even tried to decode the base64 string, and send it to google drive. didn't work as well. – Devz Feb 17 '18 at 09:18

0 Answers0