Lua
Lua
Download File from Dropbox into a String Variable
See more Dropbox Examples
Demonstrates how to download a file from Dropbox directly into a string variable.Chilkat Lua Downloads
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("Authorization", "Bearer DROPBOX_ACCESS_TOKEN")
-- The download "parameters" are contained in JSON passed in an HTTP request header.
-- This is the JSON indicating the file to be downloaded:
-- {
-- "path": "/jack.txt",
-- }
local json = chilkat.JsonObject.new()
json:append_string("path", "/jack.txt")
rest:add_header("Dropbox-API-Arg", json:emit())
-- The content of the file on Dropbox is returned.
local fileContent = rest:full_request_no_body("POST", "/2/files/download")
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): " .. fileContent)
print("---")
print("LastRequestStartLine: " .. rest.last_request_start_line)
print("LastRequestHeader: " .. rest.last_request_header)
return
end
-- Show the file content that was downloaded:
print(fileContent)
print("----")
-- Information about the downloaded file is also available as JSON in a response header.
-- The "dropbox-api-result" response header contains the information. For example:
local apiResult = rest:response_hdr_by_name("dropbox-api-result")
print(apiResult)
-- In this case, the pretty-formatted dropbox-api-result JSON 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
-- }
-- Load the JSON, pretty-print it, and demonstrate how to get some values...
local jsonResult = chilkat.JsonObject.new()
jsonResult.emit_compact = false
jsonResult:load(apiResult)
-- Show the JSON pretty-printed...
print(jsonResult:emit())
-- Sample code to get data from the JSON response:
local size = jsonResult:int_of("size")
print("size = " .. size)
local rev = jsonResult:string_of("rev")
print("rev = " .. rev)
local clientModified = jsonResult: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)