2

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.

Swagat
  • 617
  • 5
  • 16

1 Answers1

0

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--
pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • Thanks, I have followed same. But can't understand, where I am doing wrong. As per my knowledge, I sent the title in request body only. It would be great if you can pointout the exact place where I am doing wrong(either of url calling method) – Swagat Dec 07 '15 at 06:06
  • paste the actual http POST request (not your code - the request itself) into the question. All you need to do is compare what you are posting with the canonical example I pasted. – pinoyyid Dec 07 '15 at 08:06
  • Sorry, But this is how we can call google api by using requests package. It is used to call url in python script. The requests is just a package like curl in python. – Swagat Dec 07 '15 at 11:39