Sample code for 30+ languages & platforms
Lua

Upload String to Dropbox

See more Dropbox Examples

Uploads a string to a file on Dropbox. This example can upload content up to 150MB, assuming a 150MB string fits in memory for your app. Larger files must be uploaded with an upload session (upload_session/start).

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

-- A Dropbox access token should have been previously obtained.
-- Dropbox access tokens do not expire.
-- See Dropbox Access Token.

local rest = chilkat.Rest.new()

-- Connect to Dropbox
local success = rest:connect("content.dropboxapi.com", 443, true, true)
if not success then
    print(rest.last_error_text)
    return
end

-- Add request headers.
rest:add_header("Content-Type", "application/octet-stream")
rest:add_header("Authorization", "Bearer DROPBOX_ACCESS_TOKEN")

-- The upload "parameters" contained in JSON passed in an HTTP request header.
-- This is the JSON to be added in this example:
-- {
--    "path": "/jack.txt",
--    "mode": "add",
--    "autorename": true,
--    "mute": false
-- }

-- This will be the content of the file created in Dropbox.
local content = "All work and no play makes Jack a dull boy"

local json = chilkat.JsonObject.new()
json:append_string("path", "/jack.txt")
json:append_string("mode", "add")
json:append_bool("autorename", true)
json:append_bool("mute", false)
rest:add_header("Dropbox-API-Arg", json:emit())

-- Do the upload.  The URL is https://content.dropboxapi.com/2/files/upload.
-- We already connected to content.dropboxapi.com using TLS (i.e. HTTPS),
-- so now we only need to specify the path "/2/files/upload".

local responseStr = rest:full_request_string("POST", "/2/files/upload", content)
if not rest.last_method_success then
    print(rest.last_error_text)
    return
end

-- When successful, Dropbox responds with a 200 response code.
if rest.response_status_code ~= 200 then
    -- Examine the request/response to see what happened.
    print("response status code = " .. rest.response_status_code)
    print("response status text = " .. rest.response_status_text)
    print("response header: " .. rest.response_header)
    print("response body (if any): " .. responseStr)
    print("---")
    print("LastRequestStartLine: " .. rest.last_request_start_line)
    print("LastRequestHeader: " .. rest.last_request_header)
    return
end

print("LastRequestHeader: " .. rest.last_request_header)

-- The response is JSON.
local jsonResp = chilkat.JsonObject.new()
jsonResp.emit_compact = false
jsonResp:load(responseStr)

-- Show the JSON response.
print(jsonResp:emit())

-- Returns JSON that looks like this:
-- {
--  "name": "jack.txt",
--  "path_lower": "/jack.txt",
--  "path_display": "/jack.txt",
--  "id": "id:yqx4-tE_NKAAAAAAAAAAAQ",
--  "client_modified": "2016-06-02T20:42:11Z",
--  "server_modified": "2016-06-02T20:42:11Z",
--  "rev": "8482db15f",
--  "size": 42
-- }

-- Sample code to get data from the JSON response:
local size = jsonResp:int_of("size")
print("size = " .. size)

local rev = jsonResp:string_of("rev")
print("rev = " .. rev)

local clientModified = jsonResp:string_of("client_modified")
local ckdt = chilkat.DateTime.new()
ckdt:set_from_timestamp(clientModified)
local bLocalTime = true
local dt = chilkat.DtObj.new()
ckdt:to_dt_obj(bLocalTime, dt)

print(dt.day .. "/" .. dt.month .. "/" .. dt.year .. " " .. dt.hour .. ":" .. dt.minute)