teb585
teb585

Reputation: 83

How to perform a multipart upload with Google Drive REST API v3

I'm trying to make an https request to upload a file by following the guide https://developers.google.com/drive/api/v3/manage-uploads#multipart . This is what I try to send:

POST https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&key=AIz...Q4 HTTP/1.1\r\n
Authorization: Bearer ya...8A\r\n
Content-Type: multipart/related; boundary=foo_bar_baz\r\n
Content-Length: 150\r\n
\r\n
--foo_bar_baz\n
Content-Type: application/json; charset=UTF-8\n
\n
{\n
    "title": "My File.jpg"\n
}\n
\n
--foo_bar_baz\n
Content-Type: text/txt\n
\n
JPEG data\n
--foo_bar_baz--\r\n

Could you tell me what I'm wrong or a working example? Thank you

UPDATE:

Ok, after several attempts I was able to find a correct https request, for example:

POST https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&key=AI...qeQ4 HTTP/1.1\r\n
Authorization: Bearer yaSN...Q9Q\r\n
Content-Type: multipart/related; boundary=foo_bar_baz\r\n
Content-Length: 167\r\n
\r\n
--foo_bar_baz\n
Content-Type: application/json; charset=UTF-8\n
\n
{\n
    "name": "My File.txt"\n
}\n
--foo_bar_baz\n
Content-Type: text/plain; charset=UTF-8\n
\n
aaaaa\n
--foo_bar_baz--

with reply:

HTTP/1.1 200 OK\r\n
X-GUploader-UploadID: ABg...lRCA\r\n
Vary: Origin\r\n
Vary: X-Origin\r\n
Content-Type: application/json; charset=UTF-8\r\n
Cache-Control: no-cache, no-store, max-age=0, must-revalidate\r\n
Pragma: no-cache\r\n
Expires: Mon, 01 Jan 1990 00:00:00 GMT\r\n
Date: Thu, 11 Feb 2021 17:37:39 GMT\r\n
Content-Length: 121\r\n
Server: UploadServer\r\n
Alt-Svc: h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"\r\n
\r\n
{\n
 "kind": "drive#file",\n
 "id": "1_SE...8X",\n
 "name": "My File.txt",\n
 "mimeType": "text/plain"\n
}\n

I need to set the correct body length, otherwise the server returns an error.

Upvotes: 1

Views: 1246

Answers (1)

Kessy
Kessy

Reputation: 2014

The user UPDATE is the answer

Ok, after several attempts I was able to find a correct https request, for example:

POST https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&key=AI...qeQ4 HTTP/1.1\r\n
Authorization: Bearer yaSN...Q9Q\r\n
Content-Type: multipart/related; boundary=foo_bar_baz\r\n
Content-Length: 167\r\n
\r\n
--foo_bar_baz\n
Content-Type: application/json; charset=UTF-8\n
\n
{\n
    "name": "My File.txt"\n
}\n
--foo_bar_baz\n
Content-Type: text/plain; charset=UTF-8\n
\n
aaaaa\n
--foo_bar_baz--

with reply:

HTTP/1.1 200 OK\r\n
X-GUploader-UploadID: ABg...lRCA\r\n
Vary: Origin\r\n
Vary: X-Origin\r\n
Content-Type: application/json; charset=UTF-8\r\n
Cache-Control: no-cache, no-store, max-age=0, must-revalidate\r\n
Pragma: no-cache\r\n
Expires: Mon, 01 Jan 1990 00:00:00 GMT\r\n
Date: Thu, 11 Feb 2021 17:37:39 GMT\r\n
Content-Length: 121\r\n
Server: UploadServer\r\n
Alt-Svc: h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"\r\n
\r\n
{\n
 "kind": "drive#file",\n
 "id": "1_SE...8X",\n
 "name": "My File.txt",\n
 "mimeType": "text/plain"\n
}\n

I need to set the correct body length, otherwise the server returns an error.

Upvotes: 1

Related Questions