I am trying to upload file using nodejs express to google drive, Sending post request from postman, i got the error "Invalid multipart request with 0 mime parts.", Problem is within the request body i think, Any idea would be grateful to solve this issue or any suggestions, thank you.
let file = req.files.form_doc_20;
var contentType = file.type || 'application/octet-stream';
let parentId = 'root';
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var data = fs.readFileSync(file.path);
let metadata = {
title: file.name,
mimeType: contentType,
parents: [parentId]
};
var base64Data = Buffer(data, 'base64');
var multipartRequestBody =
delimiter +
"Content-Type: application/json\r\n\r\n" +
JSON.stringify(metadata) +
delimiter +
"Content-Type: " + contentType + "\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"\r\n" +
base64Data +
close_delim;
let options = {
url: 'https://www.googleapis.com/upload/drive/v3/files',
method: "POST",
headers: {
'Content-Type': "multipart/related; boundary=\"" + boundary + "\"",
'Authorization': "Bearer " + req.body.token_configuration.access_token,
},
body: multipartRequestBody,
qs: {
fields: "id, name, mimeType, modifiedTime, size",
uploadType: 'multipart'
},
json: true
};
helper.http_request(options, (err1, response) => {
if (err1) {
return res.json({ msg: 'Failed to upload the file.', error: response });
}
else {
return res.json({ result: response });
}
});
I think that your script is almost correct and your script works by modifying the following 3 points.
name
instead of title
.base64Data
of the file can be retrieved by new Buffer(data).toString('base64')
.json: true
of options
is used, the error of Invalid multipart request with 0 mime parts.
occurs. Please remove this.Please modify as follows.
title: file.name,
To:
name: file.name,
var base64Data = Buffer(data, 'base64');
To:
var base64Data = new Buffer(data).toString('base64');
Please remove json: true
from options
.
If these modifications were not useful for your situation, I apologize.