Unicode C++ Requires Chilkat v11.5.0+
Unicode C++
Box: Move a File to Trash and Restore It
See more Box Examples
Run the reusable setup once per HttpCurl object, then select names and execute the operation. The setup is repeated here only to make this published example independently runnable. Demonstrate deletion to trash and restoration of a disposable test file. This is not permanent deletion. Run only with an expendable file and an account whose trash policy permits recovery.
See the Box API guide for setup and the complete workflow.
Chilkat Unicode C++ Downloads
#include <CkHttpCurlW.h>
#include <CkStringBuilderW.h>
void ChilkatSample(void)
{
bool success = false;
// Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
success = false;
// BEGIN ONE-TIME SETUP
// This block is repeated so each published example can run on its own.
// In an application, run the setup example once and pass the SAME curl object
// to operation code below. Do not recreate it for every operation.
// See Reusable Box HttpCurl Setup.
CkHttpCurlW curl;
curl.SetVar(L"access_token",L"BOX_ACCESS_TOKEN");
curl.SetVar(L"parent_name",L"/");
curl.SetVar(L"folder_name",L"images");
curl.SetVar(L"file_name",L"penguins.jpg");
curl.SetVar(L"destination_name",L"archive");
// Box's fixed root ID is an implementation detail; callers supply names.
curl.SetVar(L"parent_id",L"0");
CkStringBuilderW setupCommand;
int setupReplacements;
// Register findParent; registration sends no request.
setupCommand.SetString(L"curl -X GET \"https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"");
setupReplacements = setupCommand.Replace(L"ACCESS_TOKEN",curl.getVar(L"access_token"));
success = curl.AddFunction(L"findParent",setupCommand.getAsString());
if (success != true) {
wprintf(L"%s\n",curl.lastErrorText());
return;
}
// Register findFolder; registration sends no request.
setupCommand.SetString(L"curl -X GET \"https://api.box.com/2.0/folders/{{parent_id}}/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"");
setupReplacements = setupCommand.Replace(L"ACCESS_TOKEN",curl.getVar(L"access_token"));
success = curl.AddFunction(L"findFolder",setupCommand.getAsString());
if (success != true) {
wprintf(L"%s\n",curl.lastErrorText());
return;
}
// Register findFile; registration sends no request.
setupCommand.SetString(L"curl -X GET \"https://api.box.com/2.0/folders/{{folder_id}}/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"");
setupReplacements = setupCommand.Replace(L"ACCESS_TOKEN",curl.getVar(L"access_token"));
success = curl.AddFunction(L"findFile",setupCommand.getAsString());
if (success != true) {
wprintf(L"%s\n",curl.lastErrorText());
return;
}
// Register findDestination; registration sends no request.
setupCommand.SetString(L"curl -X GET \"https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"");
setupReplacements = setupCommand.Replace(L"ACCESS_TOKEN",curl.getVar(L"access_token"));
success = curl.AddFunction(L"findDestination",setupCommand.getAsString());
if (success != true) {
wprintf(L"%s\n",curl.lastErrorText());
return;
}
// Register getFileDetails; registration sends no request.
setupCommand.SetString(L"curl -X GET \"https://api.box.com/2.0/files/{{file_id}}?fields=id,sha1,file_version\" -H \"Authorization: Bearer ACCESS_TOKEN\"");
setupReplacements = setupCommand.Replace(L"ACCESS_TOKEN",curl.getVar(L"access_token"));
success = curl.AddFunction(L"getFileDetails",setupCommand.getAsString());
if (success != true) {
wprintf(L"%s\n",curl.lastErrorText());
return;
}
// Register getEnterprise; registration sends no request.
setupCommand.SetString(L"curl -X GET \"https://api.box.com/2.0/users/me?fields=enterprise\" -H \"Authorization: Bearer ACCESS_TOKEN\"");
setupReplacements = setupCommand.Replace(L"ACCESS_TOKEN",curl.getVar(L"access_token"));
success = curl.AddFunction(L"getEnterprise",setupCommand.getAsString());
if (success != true) {
wprintf(L"%s\n",curl.lastErrorText());
return;
}
// Initial name selections. AddOutput2 stores these WHERE values literally.
success = curl.AddOutput2(L"findParent",L"entries",L"name",curl.getVar(L"parent_name"),true,L"id",L"parent_id");
if (success != true) {
wprintf(L"%s\n",curl.lastErrorText());
return;
}
success = curl.AddOutput2(L"findFolder",L"entries",L"name",curl.getVar(L"folder_name"),true,L"id",L"folder_id");
if (success != true) {
wprintf(L"%s\n",curl.lastErrorText());
return;
}
success = curl.AddOutput2(L"findFile",L"entries",L"name",curl.getVar(L"file_name"),true,L"id",L"file_id");
if (success != true) {
wprintf(L"%s\n",curl.lastErrorText());
return;
}
success = curl.AddOutput2(L"findDestination",L"entries",L"name",curl.getVar(L"destination_name"),true,L"id",L"destination_id");
if (success != true) {
wprintf(L"%s\n",curl.lastErrorText());
return;
}
success = curl.AddOutput(L"getFileDetails",L"file_version.id",L"file_version_id");
if (success != true) {
wprintf(L"%s\n",curl.lastErrorText());
return;
}
success = curl.AddOutput(L"getFileDetails",L"sha1",L"file_sha1");
if (success != true) {
wprintf(L"%s\n",curl.lastErrorText());
return;
}
success = curl.AddOutput(L"getEnterprise",L"enterprise.id",L"enterprise_id");
if (success != true) {
wprintf(L"%s\n",curl.lastErrorText());
return;
}
// END ONE-TIME SETUP
// The name lookups below assume each listing fits in one page (1000 items).
// Parent and destination names select root children; folder_name selects a child
// of parent_name. "/" selects the root parent. These are not arbitrary paths.
// BEGIN OPERATION -- keep the configured object when reusing it.
// Inputs are names. Retain cached IDs when the selected names have not changed.
const wchar_t *parentName = L"/";
const wchar_t *folderName = L"documents";
const wchar_t *fileName = L"disposable.txt";
if ((curl.getVar(L"parent_name") != parentName) || ((parentName == L"/") && (curl.VarDefined(L"parent_id") == false))) {
curl.SetVar(L"parent_name",parentName);
curl.ClearVar(L"parent_id");
curl.ClearVar(L"folder_id");
curl.ClearVar(L"file_id");
curl.ClearVar(L"file_version_id");
curl.ClearVar(L"file_sha1");
if (parentName == L"/") {
curl.SetVar(L"parent_id",L"0");
}
else {
success = curl.AddOutput2(L"findParent",L"entries",L"name",curl.getVar(L"parent_name"),true,L"id",L"parent_id");
if (success != true) {
wprintf(L"%s\n",curl.lastErrorText());
return;
}
}
}
if (curl.getVar(L"folder_name") != folderName) {
curl.SetVar(L"folder_name",folderName);
curl.ClearVar(L"folder_id");
curl.ClearVar(L"file_id");
curl.ClearVar(L"file_version_id");
curl.ClearVar(L"file_sha1");
success = curl.AddOutput2(L"findFolder",L"entries",L"name",curl.getVar(L"folder_name"),true,L"id",L"folder_id");
if (success != true) {
wprintf(L"%s\n",curl.lastErrorText());
return;
}
}
if (curl.getVar(L"file_name") != fileName) {
curl.SetVar(L"file_name",fileName);
curl.ClearVar(L"file_id");
curl.ClearVar(L"file_version_id");
curl.ClearVar(L"file_sha1");
success = curl.AddOutput2(L"findFile",L"entries",L"name",curl.getVar(L"file_name"),true,L"id",L"file_id");
if (success != true) {
wprintf(L"%s\n",curl.lastErrorText());
return;
}
}
curl.ClearTargetOutput(L"*");
CkStringBuilderW authorizedCommand1;
authorizedCommand1.SetString(L"curl -X DELETE \"https://api.box.com/2.0/files/{{file_id}}\" -H \"Authorization: Bearer {{access_token}}\"");
int headerReplacement1 = authorizedCommand1.Replace(L"{{access_token}}",curl.getVar(L"access_token"));
success = curl.DoYourThing(authorizedCommand1.getAsString());
if ((success != true) || (curl.get_StatusCode() != 204)) {
wprintf(L"Box request failed. HTTP status: %d\n",curl.get_StatusCode());
wprintf(L"Failure reason: %d\n",curl.get_FailReason());
wprintf(L"%s\n",curl.responseBodyStr());
return;
}
curl.ClearTargetOutput(L"*");
CkStringBuilderW authorizedCommand2;
authorizedCommand2.SetString(L"curl -X POST \"https://api.box.com/2.0/files/{{file_id}}\" -H \"Authorization: Bearer {{access_token}}\" -H \"Content-Type: application/json\" -d '{}'");
int headerReplacement2 = authorizedCommand2.Replace(L"{{access_token}}",curl.getVar(L"access_token"));
success = curl.DoYourThing(authorizedCommand2.getAsString());
if ((success != true) || (curl.get_StatusCode() != 201)) {
wprintf(L"Box request failed. HTTP status: %d\n",curl.get_StatusCode());
wprintf(L"Failure reason: %d\n",curl.get_FailReason());
wprintf(L"%s\n",curl.responseBodyStr());
return;
}
wprintf(L"File restored: %s\n",curl.responseBodyStr());
}