Search code examples
pythoncurlgoogle-drive-apigoogle-drive-realtime-api

Google drive upload with untitled name


May be this is a duplicate question but I didn't get any successful answer for me.

I am trying to upload some file to google drive. It getting successfully upload but the file name is remain "Untitled". Here is bellow what I have tried in curl and python requests

curl

curl -X POST -H 'Content-Type:*/*' 
    -H 'Authorization: Bearer <The access token>' 
    -H 'Content-Length:125' 
    --upload-file 'foo.txt' 
    'https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart' 
    -d '{"title":"foo.txt","description":"test file"}'

Here while trying with Content-Type:multipart/related; boundary="foo_bar_baz" its throwing error saying "use multipart" while trying only "multipart" its saying to use "/" . Any how file is getting uploaded but name is "Untitled"

Here is bellow same thing in python request:

upload_files = requests.post(
  'https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart',
  headers={'Authorization':'Bearer {}'.format(access_token),
           'Content-Type':'*/*', 
           'Content-Length':file_size},
  data={"title":file_name,
        "mimeType": "*/*",
        "parents": [{"id":"root"}],
        "description": "mypdf.pdf"},
  files={'file':file},

)

Please dont downvote it, if I did a silly mistake, instead put a comment. I will check it. Here In my situation I cant use google drive client Thanks a lot for giving time to check my problem.


Solution

  • You aren't correctly constructing the multipart body. See https://developers.google.com/drive/web/manage-uploads#multipart for an example

    POST /upload/drive/v2/files?uploadType=multipart HTTP/1.1
    Host: www.googleapis.com
    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
    
    {
      "title": "My File"
    }
    
    --foo_bar_baz
    Content-Type: image/jpeg
    
    JPEG data
    --foo_bar_baz--