简体   繁体   中英

How to fix Invalid multipart request with 0 mime parts error

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.

Modification points:

  1. When you use Drive API v3, please use the property of name instead of title .
  2. base64Data of the file can be retrieved by new Buffer(data).toString('base64') .
  3. When json: true of options is used, the error of Invalid multipart request with 0 mime parts. occurs. Please remove this.

Modified script:

Please modify as follows.

1:

From:
 title: file.name, 
To:
 name: file.name, 

2:

From:
 var base64Data = Buffer(data, 'base64'); 
To:
 var base64Data = new Buffer(data).toString('base64'); 

3:

Please remove json: true from options .

Note:

  • This modified script supposes that your environment can upload files using Drive API. If the error related to API occurs, please confirm whether Drive API is enabled. And also please confirm the scopes including in the access token.
  • I could confirm that the modified script can upload a file to Google Drive. So if other error occurs, it is considered that the other part which is not this script is the reason.

References:

If these modifications were not useful for your situation, I apologize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM