Delphi ActiveX Dropbox: Dropbox Create Folder

Back to Index

Create a folder. Creates the folder /Halloween/emojis

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

CURL Command

curl -X POST https://api.dropboxapi.com/2/files/create_folder_v2 \
    --header "Authorization: Bearer DROPBOX-ACCESS-TOKEN" \
    --header "Content-Type: application/json" \
    --data "{\"path\": \"/Halloween/emojis\",\"autorename\": false}"

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;
j: Integer;
count_j: Integer;
metadataName: WideString;
metadataId: WideString;
metadataPath_lower: WideString;
metadataPath_display: WideString;
metadataSharing_infoRead_only: Integer;
metadataSharing_infoParent_shared_folder_id: WideString;
metadataSharing_infoTraverse_only: Integer;
metadataSharing_infoNo_access: Integer;
template_id: WideString;
name: WideString;
value: WideString;

begin
rest := TChilkatRest.Create(Self);

//  URL: https://api.dropboxapi.com/2/files/create_folder_v2
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','/Halloween/emojis');
json.UpdateBool('autorename',0);

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/create_folder_v2',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

metadataName := jsonResponse.StringOf('metadata.name');
metadataId := jsonResponse.StringOf('metadata.id');
metadataPath_lower := jsonResponse.StringOf('metadata.path_lower');
metadataPath_display := jsonResponse.StringOf('metadata.path_display');
metadataSharing_infoRead_only := jsonResponse.BoolOf('metadata.sharing_info.read_only');
metadataSharing_infoParent_shared_folder_id := jsonResponse.StringOf('metadata.sharing_info.parent_shared_folder_id');
metadataSharing_infoTraverse_only := jsonResponse.BoolOf('metadata.sharing_info.traverse_only');
metadataSharing_infoNo_access := jsonResponse.BoolOf('metadata.sharing_info.no_access');
i := 0;
count_i := jsonResponse.SizeOfArray('metadata.property_groups');
while i < count_i do
  begin
    jsonResponse.I := i;
    template_id := jsonResponse.StringOf('metadata.property_groups[i].template_id');
    j := 0;
    count_j := jsonResponse.SizeOfArray('metadata.property_groups[i].fields');
    while j < count_j do
      begin
        jsonResponse.J := j;
        name := jsonResponse.StringOf('metadata.property_groups[i].fields[j].name');
        value := jsonResponse.StringOf('metadata.property_groups[i].fields[j].value');
        j := j + 1;
      end;

    i := i + 1;
  end;

Sample JSON Response Body

{
  "metadata": {
    "name": "emojis",
    "id": "id:a4ayc_80_OEAAAAAAAAAXz",
    "path_lower": "/halloween/emojis",
    "path_display": "/Halloween/emojis",
    "sharing_info": {
      "read_only": false,
      "parent_shared_folder_id": "84528192421",
      "traverse_only": false,
      "no_access": false
    },
    "property_groups": [
      {
        "template_id": "ptid:1a5n2i6d3OYEAAAAAAAAAYa",
        "fields": [
          {
            "name": "Security Policy",
            "value": "Confidential"
          }
        ]
      }
    ]
  }
}