Sample code for 30+ languages & platforms
Tcl

Google Drive Multipart Upload String

See more REST Examples

Demonstrates a file upload to Google Drive where the contents of the file are contained in a string variable.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# This example will upload a file to Google Drive.
set success 1

# It requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

set gAuth [new_CkAuthGoogle]

CkAuthGoogle_put_Scope $gAuth "https://www.googleapis.com/auth/drive"
CkAuthGoogle_put_SubEmailAddress $gAuth "some.user@example.com"
CkAuthGoogle_put_ExpireNumSeconds $gAuth 3600

# Obtain an access token as shown in one of the following examples:
# See Get Access Token using a Service Account JSON Key
# See Get Access Token using a P12 File

set rest [new_CkRest]

# Connect using TLS.
set bAutoReconnect 1
set success [CkRest_Connect $rest "www.googleapis.com" 443 1 $bAutoReconnect]

# Provide the authentication credentials (i.e. the access key)
set success [CkRest_SetAuthGoogle $rest $gAuth]

# A multipart upload to Google Drive needs a multipart/related Content-Type
set success [CkRest_AddHeader $rest "Content-Type" "multipart/related"]

# Specify each part of the request.

# The 1st part is JSON with information about the file.
CkRest_put_PartSelector $rest "1"
set success [CkRest_AddHeader $rest "Content-Type" "application/json; charset=UTF-8"]

set json [new_CkJsonObject]

set success [CkJsonObject_AddStringAt $json -1 "title" "helloWorld.txt"]
set success [CkJsonObject_AddStringAt $json -1 "description" "A simple text file that says Hello World."]
set success [CkJsonObject_AddStringAt $json -1 "mimeType" "text/plain"]
set success [CkRest_SetMultipartBodyString $rest [CkJsonObject_emit $json]]

# The 2nd part is the file content.
# In this case, we'll upload a simple text file containing "Hello World!"
CkRest_put_PartSelector $rest "2"
set success [CkRest_AddHeader $rest "Content-Type" "text/plain"]
set success [CkRest_SetMultipartBodyString $rest "Hello World!"]

# POST https://www.googleapis.com/upload/drive/v2/files
set jsonResponse [CkRest_fullRequestMultipart $rest "POST" "/upload/drive/v2/files?uploadType=multipart"]
if {[CkRest_get_LastMethodSuccess $rest] != 1} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkAuthGoogle $gAuth
    delete_CkRest $rest
    delete_CkJsonObject $json
    exit
}

# Show the JSON response.
puts "Response Status Code: [CkRest_get_ResponseStatusCode $rest]"
puts "Json Response: $jsonResponse"

delete_CkAuthGoogle $gAuth
delete_CkRest $rest
delete_CkJsonObject $json