Delphi ActiveX Dropbox: Dropbox List Contents of Folder

Back to Index

Starts returning the contents of a folder. If the result's ListFolderResult.has_more field is true, call list_folder/continue with the returned ListFolderResult.cursor to retrieve more entries.

Documentation: https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder

CURL Command

curl -X POST https://api.dropboxapi.com/2/files/list_folder \
    --header "Authorization: Bearer DROPBOX-ACCESS-TOKEN" \
    --header "Content-Type: application/json" \
    --data "{\"path\": \"/Homework/math\",\"recursive\": false,\"include_media_info\": false,\"include_deleted\": false,\"include_has_explicit_shared_members\": false,\"include_mounted_folders\": true}"

Delphi ActiveX Example

var
rest: TChilkatRest;
success: Integer;
bTls: Integer;
port: Integer;
bAutoReconnect: Integer;
json: TChilkatJsonObject;
sbRequestBody: TChilkatStringBuilder;
sbResponseBody: TChilkatStringBuilder;
respStatusCode: Integer;
jsonResponse: TChilkatJsonObject;
i: Integer;
count_i: Integer;
cursor: WideString;
has_more: Integer;
Tag: WideString;
name: WideString;
path_lower: WideString;
path_display: WideString;
id: WideString;
client_modified: WideString;
server_modified: WideString;
rev: WideString;
size: Integer;
content_hash: WideString;

begin
rest := TChilkatRest.Create(Self);

//  URL: https://api.dropboxapi.com/2/files/list_folder
bTls := 1;
port := 443;
bAutoReconnect := 1;
success := rest.Connect('api.dropboxapi.com',port,bTls,bAutoReconnect);
if (success <> 1) then
  begin
    Memo1.Lines.Add('ConnectFailReason: ' + IntToStr(rest.ConnectFailReason));
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;

//  See the Online Tool for Generating JSON Creation Code
json := TChilkatJsonObject.Create(Self);
json.UpdateString('path','/Homework/math');
json.UpdateBool('recursive',0);
json.UpdateBool('include_media_info',0);
json.UpdateBool('include_deleted',0);
json.UpdateBool('include_has_explicit_shared_members',0);
json.UpdateBool('include_mounted_folders',1);

rest.AddHeader('Authorization','Bearer DROPBOX-ACCESS-TOKEN');
rest.AddHeader('Content-Type','application/json');

sbRequestBody := TChilkatStringBuilder.Create(Self);
json.EmitSb(sbRequestBody.ControlInterface);
sbResponseBody := TChilkatStringBuilder.Create(Self);
success := rest.FullRequestSb('POST','/2/files/list_folder',sbRequestBody.ControlInterface,sbResponseBody.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;
respStatusCode := rest.ResponseStatusCode;
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(rest.ResponseHeader);
    Memo1.Lines.Add('Response Body:');
    Memo1.Lines.Add(sbResponseBody.GetAsString());
    Exit;
  end;

jsonResponse := TChilkatJsonObject.Create(Self);
jsonResponse.LoadSb(sbResponseBody.ControlInterface);

//  See the Online Tool for Generating JSON Parse Code

cursor := jsonResponse.StringOf('cursor');
has_more := jsonResponse.BoolOf('has_more');
i := 0;
count_i := jsonResponse.SizeOfArray('entries');
while i < count_i do
  begin
    jsonResponse.I := i;
    Tag := jsonResponse.StringOf('entries[i].".tag"');
    name := jsonResponse.StringOf('entries[i].name');
    path_lower := jsonResponse.StringOf('entries[i].path_lower');
    path_display := jsonResponse.StringOf('entries[i].path_display');
    id := jsonResponse.StringOf('entries[i].id');
    client_modified := jsonResponse.StringOf('entries[i].client_modified');
    server_modified := jsonResponse.StringOf('entries[i].server_modified');
    rev := jsonResponse.StringOf('entries[i].rev');
    size := jsonResponse.IntOf('entries[i].size');
    content_hash := jsonResponse.StringOf('entries[i].content_hash');
    i := i + 1;
  end;

Sample JSON Response Body

{
  "entries": [
    {
      ".tag": "file",
      "name": "Matrices.txt",
      "path_lower": "/homework/math/matrices.txt",
      "path_display": "/Homework/math/Matrices.txt",
      "id": "id:qk7WwvROeSAAAAAAAAAAAQ",
      "client_modified": "2016-06-02T20:41:02Z",
      "server_modified": "2016-06-02T20:41:03Z",
      "rev": "5482db15f",
      "size": 6,
      "content_hash": "5a3c776e2631edabe2ba710ac72301b3aeca821ba4f7d36b56cc6050ea6c2ba8"
    },
    {
      ".tag": "file",
      "name": "Document.docx",
      "path_lower": "/homework/math/document.docx",
      "path_display": "/Homework/math/Document.docx",
      "id": "id:JSXYsxHo1hAAAAAAAAAABw",
      "client_modified": "2018-10-22T21:59:23Z",
      "server_modified": "2018-10-22T21:59:23Z",
      "rev": "10482db15f",
      "size": 11009,
      "content_hash": "ab9c1887a653a8f42e4401425ac02839e1f76533e1d44ac8d0605d870a16c25d"
    }
  ],
  "cursor": "AAHnmmjY1IztwebFmkByuZ_Vgzz-tSYXYE3KhdkUxBfETpGjPqTBz7ZzBj1G7zkPPVanKCt0FtzqXjDQ2ggQKYZTiL4mJ346fRelmP049QjN5CZzeMHbD_lSFqA96Br36vR9YLg40VAHu42TcCG38MbUPOCKa-wdguu5C1JsgYvIaXIcjRMCfbxz98JdYhhefmu0pjoxtgtCstOrmp_pXIng",
  "has_more": false
}