Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oGAuth
LOCAL oRest
LOCAL nBAutoReconnect
LOCAL oJson
LOCAL cJsonResponse

nSuccess := 0

//  This example will upload a file to Google Drive.
nSuccess := 1

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

oGAuth := CreateObject("Chilkat.AuthGoogle")
oGAuth:Scope := "https://www.googleapis.com/auth/drive"
oGAuth:SubEmailAddress := "some.user@example.com"
oGAuth:ExpireNumSeconds := 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

oRest := CreateObject("Chilkat.Rest")

//  Connect using TLS.
nBAutoReconnect := 1
nSuccess := oRest:Connect("www.googleapis.com", 443, 1, nBAutoReconnect)

//  Provide the authentication credentials (i.e. the access key)
nSuccess := oRest:SetAuthGoogle(oGAuth)

//  A multipart upload to Google Drive needs a multipart/related Content-Type
nSuccess := oRest:AddHeader("Content-Type", "multipart/related")

//  Specify each part of the request.

//  The 1st part is JSON with information about the file.
oRest:PartSelector := "1"
nSuccess := oRest:AddHeader("Content-Type", "application/json; charset=UTF-8")

oJson := CreateObject("Chilkat.JsonObject")
nSuccess := oJson:AddStringAt(-1, "title", "helloWorld.txt")
nSuccess := oJson:AddStringAt(-1, "description", "A simple text file that says Hello World.")
nSuccess := oJson:AddStringAt(-1, "mimeType", "text/plain")
nSuccess := oRest:SetMultipartBodyString(oJson:Emit())

//  The 2nd part is the file content.
//  In this case, we'll upload a simple text file containing "Hello World!"
oRest:PartSelector := "2"
nSuccess := oRest:AddHeader("Content-Type", "text/plain")
nSuccess := oRest:SetMultipartBodyString("Hello World!")

//  POST https://www.googleapis.com/upload/drive/v2/files
cJsonResponse := oRest:FullRequestMultipart("POST", "/upload/drive/v2/files?uploadType=multipart")
IF (oRest:LastMethodSuccess != 1)
    ? oRest:LastErrorText
    oGAuth:destroy()
    oRest:destroy()
    oJson:destroy()
    RETURN
ENDIF

//  Show the JSON response.
? "Response Status Code: " + Str(oRest:ResponseStatusCode)
? "Json Response: " + cJsonResponse

oGAuth:destroy()
oRest:destroy()
oJson:destroy()