Search code examples
google-apigoogle-drive-apipostmanmultipart

Google Drive API multipart upload gets file corrupted


I've been trying to upload a file using the Google Drive API but I'm facing an issue and I can't seem to find the answer. This happens when uploading an image, with text files, it seems to be ok.

Following the documentation here: https://developers.google.com/drive/api/v3/manage-uploads I need to form the request like this

Google Drive API

I've tried either from POSTMAN or from Salesforce Apex and got the same issue.

I'm sending the image encoded as Base64, but the images are not getting decoded on the Google Drive side, so the issue is that there is no preview and can't open the file. If I download the file and open it on a text editor, the content is just text.

postman

This is the image opened from VS Code

enter image description here


Solution

  • I think that you are trying to upload the base64 data. So how about this modification?

    Modified request body:

    POST https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart HTTP/1.1
    Authorization: Bearer [YOUR_AUTH_TOKEN]
    Content-Type: multipart/related; boundary=foo_bar_baz
    Content-Length: [NUMBER_OF_BYTES_IN_ENTIRE_REQUEST_BODY]
    
    --foo_bar_baz
    Content-Type: application/json; charset=UTF-8
    
    {
      "name": "myObject"
    }
    
    --foo_bar_baz
    Content-Type: image/jpeg
    Content-Transfer-Encoding: base64   <--- Added this.
    
    [JPEG_DATA]
    --foo_bar_baz--
    
    • Please add Content-Transfer-Encoding: base64 to the data part.

    Reference:

    If this didn't resolve your issue, I apologize.