Sample code for 30+ languages & platforms
Xbase++

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

Xbase++
LOCAL nSuccess
LOCAL oRest
LOCAL oJson
LOCAL cFileContent
LOCAL cApiResult
LOCAL oJsonResult
LOCAL nSize
LOCAL cRev
LOCAL cClientModified
LOCAL oCkdt
LOCAL nBLocalTime
LOCAL oDt

nSuccess := 0

//  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.

oRest := CreateObject("Chilkat.Rest")

//  Connect to Dropbox
nSuccess := oRest:Connect("content.dropboxapi.com", 443, 1, 1)
IF (nSuccess == 0)
    ? oRest:LastErrorText
    oRest:destroy()
    RETURN
ENDIF

//  Add request headers.
oRest:AddHeader("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",
//  }

oJson := CreateObject("Chilkat.JsonObject")
oJson:AppendString("path", "/jack.txt")
oRest:AddHeader("Dropbox-API-Arg", oJson:Emit())

//  The content of the file on Dropbox is returned.
cFileContent := oRest:FullRequestNoBody("POST", "/2/files/download")
IF (oRest:LastMethodSuccess == 0)
    ? oRest:LastErrorText
    oRest:destroy()
    oJson:destroy()
    RETURN
ENDIF

//  When successful, Dropbox responds with a 200 response code.
IF (oRest:ResponseStatusCode != 200)
    //  Examine the request/response to see what happened.
    ? "response status code = " + Str(oRest:ResponseStatusCode)
    ? "response status text = " + oRest:ResponseStatusText
    ? "response header: " + oRest:ResponseHeader
    ? "response body (if any): " + cFileContent
    ? "---"
    ? "LastRequestStartLine: " + oRest:LastRequestStartLine
    ? "LastRequestHeader: " + oRest:LastRequestHeader
    oRest:destroy()
    oJson:destroy()
    RETURN
ENDIF

//  Show the file content that was downloaded:
? cFileContent
? "----"

//  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:
cApiResult := oRest:ResponseHdrByName("dropbox-api-result")
? cApiResult

//  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...
oJsonResult := CreateObject("Chilkat.JsonObject")
oJsonResult:EmitCompact := 0
oJsonResult:Load(cApiResult)
//  Show the JSON pretty-printed...
? oJsonResult:Emit()

//  Sample code to get data from the JSON response:
nSize := oJsonResult:IntOf("size")
? "size = " + Str(nSize)

cRev := oJsonResult:StringOf("rev")
? "rev = " + cRev

cClientModified := oJsonResult:StringOf("client_modified")
oCkdt := CreateObject("Chilkat.CkDateTime")
oCkdt:SetFromTimestamp(cClientModified)
nBLocalTime := 1
oDt := CreateObject("Chilkat.DtObj")
oCkdt:ToDtObj(nBLocalTime, oDt)

? Str(oDt:Day) + "/" + Str(oDt:Month) + "/" + Str(oDt:Year) + " " + Str(oDt:Hour) + ":" + Str(oDt:Minute)

oRest:destroy()
oJson:destroy()
oJsonResult:destroy()
oCkdt:destroy()
oDt:destroy()