Sample code for 30+ languages & platforms
Lua

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 Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example will upload a file to Google Drive.
local success = true

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

local gAuth = chilkat.AuthGoogle.new()
gAuth.scope = "https://www.googleapis.com/auth/drive"
gAuth.sub_email_address = "some.user@example.com"
gAuth.expire_num_seconds = 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

local rest = chilkat.Rest.new()

-- Connect using TLS.
local bAutoReconnect = true
success = rest:connect("www.googleapis.com", 443, true, bAutoReconnect)

-- Provide the authentication credentials (i.e. the access key)
success = rest:set_auth_google(gAuth)

-- A multipart upload to Google Drive needs a multipart/related Content-Type
success = rest:add_header("Content-Type", "multipart/related")

-- Specify each part of the request.

-- The 1st part is JSON with information about the file.
rest.part_selector = "1"
success = rest:add_header("Content-Type", "application/json; charset=UTF-8")

local json = chilkat.JsonObject.new()
success = json:add_string_at(-1, "title", "helloWorld.txt")
success = json:add_string_at(-1, "description", "A simple text file that says Hello World.")
success = json:add_string_at(-1, "mimeType", "text/plain")
success = rest:set_multipart_body_string(json:emit())

-- The 2nd part is the file content.
-- In this case, we'll upload a simple text file containing "Hello World!"
rest.part_selector = "2"
success = rest:add_header("Content-Type", "text/plain")
success = rest:set_multipart_body_string("Hello World!")

-- POST https://www.googleapis.com/upload/drive/v2/files
local jsonResponse = rest:full_request_multipart("POST", "/upload/drive/v2/files?uploadType=multipart")
if not rest.last_method_success then
    print(rest.last_error_text)
    return
end

-- Show the JSON response.
print("Response Status Code: " .. rest.response_status_code)
print("Json Response: " .. jsonResponse)