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.
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?