Delphi ActiveX Dropbox: Dropbox Copy a File or Folder

Back to Index

Copy a file or folder to a different location in the user's Dropbox. If the source path is a folder all its contents will be copied.

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

CURL Command

curl -X POST https://api.dropboxapi.com/2/files/copy_v2 \
    --header "Authorization: Bearer DROPBOX-ACCESS-TOKEN" \
    --header "Content-Type: application/json" \
    --data "{\"from_path\": \"/ocean/starfish.jpg\",\"to_path\": \"/ocean/starfish2.jpg\",\"allow_shared_folder\": false,\"autorename\": false,\"allow_ownership_transfer\": 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;
metadataTag: WideString;
metadataName: WideString;
metadataPath_lower: WideString;
metadataPath_display: WideString;
metadataId: WideString;
metadataClient_modified: WideString;
metadataServer_modified: WideString;
metadataRev: WideString;
metadataSize: Integer;
metadataContent_hash: WideString;

begin
rest := TChilkatRest.Create(Self);

//  URL: https://api.dropboxapi.com/2/files/copy_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('from_path','/ocean/starfish.jpg');
json.UpdateString('to_path','/ocean/starfish2.jpg');
json.UpdateBool('allow_shared_folder',0);
json.UpdateBool('autorename',0);
json.UpdateBool('allow_ownership_transfer',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/copy_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

metadataTag := jsonResponse.StringOf('metadata.".tag"');
metadataName := jsonResponse.StringOf('metadata.name');
metadataPath_lower := jsonResponse.StringOf('metadata.path_lower');
metadataPath_display := jsonResponse.StringOf('metadata.path_display');
metadataId := jsonResponse.StringOf('metadata.id');
metadataClient_modified := jsonResponse.StringOf('metadata.client_modified');
metadataServer_modified := jsonResponse.StringOf('metadata.server_modified');
metadataRev := jsonResponse.StringOf('metadata.rev');
metadataSize := jsonResponse.IntOf('metadata.size');
metadataContent_hash := jsonResponse.StringOf('metadata.content_hash');

Sample JSON Response Body

{
  "metadata": {
    ".tag": "file",
    "name": "starfish2.jpg",
    "path_lower": "/ocean/starfish2.jpg",
    "path_display": "/ocean/starfish2.jpg",
    "id": "id:JSXYsxHo1hAAAAAAAAAADg",
    "client_modified": "2018-10-22T22:38:18Z",
    "server_modified": "2018-10-22T22:40:38Z",
    "rev": "1c482db15f",
    "size": 6229,
    "content_hash": "9fa9d692d0762ee759c22dea993c7de0068f9ba2ca842e761a1636d3b0b3cbba"
  }
}