简体   繁体   中英

Ruby: Rest-client multipart upload to Google drive with metadata

I am trying to perform a file upload to google drive's API using Rest calls. The API says that if you want to name the file metadata must be passed and it should be uploaded as multipart.

I am getting a '400 Bad Request' error. I think that this might be due to having multiple content types using Rest client.

RestClient::Request.execute(
                               :method => "post",
                               :url => "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart",
                               :headers => {:Authorization => "Bearer #{access_token}", :content_type => 'multipart/related'},
                               :payload => {:metadata => "{'title' : 'testing'}", :file => File.new(file, 'rb'), :multipart => true}
                           )

Any help would be great! Thanks!

I believe, using the given library (rest-client), this is not possible as the request, according to Google's requirements, the multi-parts need to be ordered and must have the correct mime type for each of the multipart.

So, I'd say, the syntax you've used is closest to correct, but unfortunately the gem doesn't seem to support this [1] as of the time of this comment.

If you still are looking for a solution, the closest thing I found was digging directly into Google's API client library, where they hand-craft a multipart request with the help of hurley [2].

You can check the source code for some ideas.[3]

Hope this helps.

[1] https://github.com/rest-client/rest-client/pull/222

[2] https://github.com/lostisland/hurley

[3] https://github.com/google/google-api-ruby-client/blob/d2e51b4e7d4cb5a18cb08b2aed9c0d8ffff14b22/lib/google/apis/core/multipart.rb

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