1
votes

I am trying to follow the resumable media upload for google drive api from this link. https://developers.google.com/drive/manage-uploads#resumable

My first request goes through fine and I get back a second url in the Location header. However, when I make the second PUT call given in the documentation, it throws back a bad request.

Request 1: 

POST https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable

Request Header 1: 

Content-Type: application/json
Authorization: Bearer ya..
X-Upload-Content-Type: text/plain
X-Upload-Content-Length: 112
Host: www.googleapis.com
Content-Length: 112
Body 

{'title' : 'bravo122314.txt', 'mimeType': 'text/plain', 'parents' : [{'id': '0Bw5JasxEfsasa8NNsdU5dU123iVHc'}] } 


Response Header 1:
Location: https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&upload_id=ASM
Date: Tue, 03 Dec 2013 21:07:40 GMT
Server: HTTP Upload Server Built on Nov 15 2013 16:02:54 (1384560174)
Content-Length: 0
Content-Type: text/html; charset=UTF-8

This is the following request

Request 2: 
PUT https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&upload_id=ASM

Request Header 2: 
Authorization: Bearer ya..
Content-Type: text/plain


Response 2:

{
"error":{
"errors":[
{
"domain":"global",
"reason":"badRequest",
"message":"Invalid Upload Request"
}
],
"code":400,
"message":"Invalid Upload Request"
}
}

Has anyone faced this issue before? Am I missing some header?

3

3 Answers

1
votes

Hopefully you've solved this by now, but since I just started attempting to do this, I'll add some of my personal findings here.

The first thing that jumps out at me is X-Upload-Content-Length: 112 should be the length of the file you're going to send in your PUT not the length of your first request.

I also needed to add "Content-Encoding": "base64" as a header in my PUT and then make sure you encode your blob to base64. I've been doing this in javascript so I wrap my FileReader's result with btoa(reader.result)

Both of these headers need to match the Content-Type and Content-Length headers on your PUT request:

X-Upload-Content-Type: text/plain
X-Upload-Content-Length: 112

For security reasons, you can't set Content-Type yourself in a javascript request, but the browser should be able to handle that.

0
votes

Try "uploadType" use "media" to ensure can upload first then try "resumable"?

PUT https://www.googleapis.com/upload/drive/v2/files/{id}?uploadType=media

Notice that id, you can see the ref.

ref: How do I add/create/insert files to Google Drive through the API?

0
votes

In the second request, u also need to set Content-Length header, it will look like:

Request 2: 
PUT https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&upload_id=ASM

Request Header 2: 
Authorization: Bearer ya..
Content-Type: text/plain
Content-Length: 112

Request Body 2: 
{ bytes of file to upload }