Objective-C Requires Chilkat v11.7.0+
Objective-C
Box: Resolve Folder and File IDs with the HttpCurl Dependency Engine
See more Box Examples
Download images/penguins.jpg using search-based HttpCurl dependencies and one DoYourThing call. Register search requests/output rules once, then supply names. Search results must each contain exactly one item; exact names and immediate parent IDs are checked before saving the downloaded bytes. The candidate content is downloaded into memory before validation, so this example is for small files. Multiple results are rejected instead of paginated or silently choosing a candidate. Search indexing can delay visibility of newly created or renamed items.
See the Box API guide for setup and the complete workflow.
Chilkat Objective-C Downloads
#import <CkoHttpCurl.h>
#import <CkoStringBuilder.h>
#import <NSString.h>
#import <CkoJsonObject.h>
#import <CkoBinData.h>
BOOL success = NO;
// Requires Chilkat 11.7.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
success = NO;
// BEGIN ONE-TIME SETUP
// Requires Chilkat 11.7.0. Registration sends no requests.
// Run this once and keep the SAME HttpCurl object for later downloads.
// This focused setup searches for a root folder and a file directly inside it.
CkoHttpCurl *curl = [[CkoHttpCurl alloc] init];
[curl SetVar: @"access_token" varValue: @"BOX_ACCESS_TOKEN"];
CkoStringBuilder *setupCommand = [[CkoStringBuilder alloc] init];
int setupReplacements;
// searchFolder: Box searches names; quotes request a phrase match.
[setupCommand SetString: @"curl -X GET \"https://api.box.com/2.0/search?query={{folder_query}}&type=folder&content_types=name&fields=id,type,name,parent&limit=2\" -H \"Authorization: Bearer ACCESS_TOKEN\""];
setupReplacements = [[setupCommand Replace: @"ACCESS_TOKEN" replacement: [curl GetVar: @"access_token"]] intValue];
success = [curl AddFunction: @"searchFolder" curl: [setupCommand GetAsString]];
if (success != YES) {
NSLog(@"%@",curl.LastErrorText);
return;
}
// Deferred selection: folder_name need not be set during setup.
success = [curl AddOutput2: @"searchFolder" arrayPath: @"entries" wherePath: @"name" whereValue: @"{{folder_name}}" caseSensitive: YES subPath: @"id" varName: @"folder_id"];
if (success != YES) {
NSLog(@"%@",curl.LastErrorText);
return;
}
success = [curl AddOutput2: @"searchFolder" arrayPath: @"entries" wherePath: @"name" whereValue: @"{{folder_name}}" caseSensitive: YES subPath: @"name" varName: @"folder_result_name"];
if (success != YES) {
NSLog(@"%@",curl.LastErrorText);
return;
}
success = [curl AddOutput2: @"searchFolder" arrayPath: @"entries" wherePath: @"name" whereValue: @"{{folder_name}}" caseSensitive: YES subPath: @"parent.id" varName: @"folder_parent_id"];
if (success != YES) {
NSLog(@"%@",curl.LastErrorText);
return;
}
success = [curl AddOutput: @"searchFolder" jsonPath: @"total_count" varName: @"folder_search_count"];
if (success != YES) {
NSLog(@"%@",curl.LastErrorText);
return;
}
// searchFile: Box searches names; quotes request a phrase match.
[setupCommand SetString: @"curl -X GET \"https://api.box.com/2.0/search?query={{file_query}}&type=file&content_types=name&fields=id,type,name,parent&limit=2&ancestor_folder_ids={{folder_id}}\" -H \"Authorization: Bearer ACCESS_TOKEN\""];
setupReplacements = [[setupCommand Replace: @"ACCESS_TOKEN" replacement: [curl GetVar: @"access_token"]] intValue];
success = [curl AddFunction: @"searchFile" curl: [setupCommand GetAsString]];
if (success != YES) {
NSLog(@"%@",curl.LastErrorText);
return;
}
// Deferred selection: file_name need not be set during setup.
success = [curl AddOutput2: @"searchFile" arrayPath: @"entries" wherePath: @"name" whereValue: @"{{file_name}}" caseSensitive: YES subPath: @"id" varName: @"file_id"];
if (success != YES) {
NSLog(@"%@",curl.LastErrorText);
return;
}
success = [curl AddOutput2: @"searchFile" arrayPath: @"entries" wherePath: @"name" whereValue: @"{{file_name}}" caseSensitive: YES subPath: @"name" varName: @"file_result_name"];
if (success != YES) {
NSLog(@"%@",curl.LastErrorText);
return;
}
success = [curl AddOutput2: @"searchFile" arrayPath: @"entries" wherePath: @"name" whereValue: @"{{file_name}}" caseSensitive: YES subPath: @"parent.id" varName: @"file_parent_id"];
if (success != YES) {
NSLog(@"%@",curl.LastErrorText);
return;
}
success = [curl AddOutput: @"searchFile" jsonPath: @"total_count" varName: @"file_search_count"];
if (success != YES) {
NSLog(@"%@",curl.LastErrorText);
return;
}
// END ONE-TIME SETUP
// Search is indexed: newly uploaded/renamed items may not appear immediately.
// Search matches phrases, not exact strings, and ancestor filtering includes
// subfolders. The operation validates counts, exact names, and immediate parents.
// limit=2 is enough to demonstrate an ambiguous search; total_count covers all
// results, not just this page. This introductory example requires exactly one
// search result at each step and does not select among multiple candidates.
// BEGIN OPERATION -- use the previously configured object.
// Human-readable inputs. No resource IDs are supplied by the caller.
NSString *folderName = @"images";
NSString *fileName = @"penguins.jpg";
// Encode the quoted search queries separately from the literal output filters.
// In this build, URL variable substitution does not encode '&' in a name.
CkoStringBuilder *queryText = [[CkoStringBuilder alloc] init];
[queryText SetString: @"\""];
[queryText Append: folderName];
[queryText Append: @"\""];
[curl SetVar: @"folder_query" varValue: [queryText GetEncoded: @"url" charset: @"utf-8"]];
[queryText SetString: @"\""];
[queryText Append: fileName];
[queryText Append: @"\""];
[curl SetVar: @"file_query" varValue: [queryText GetEncoded: @"url" charset: @"utf-8"]];
if ([curl GetVar: @"folder_name"] != folderName) {
[curl ClearVar: @"folder_id"];
[curl ClearVar: @"folder_result_name"];
[curl ClearVar: @"folder_parent_id"];
[curl ClearVar: @"folder_search_count"];
[curl ClearVar: @"file_id"];
[curl ClearVar: @"file_result_name"];
[curl ClearVar: @"file_parent_id"];
[curl ClearVar: @"file_search_count"];
[curl SetVar: @"folder_name" varValue: folderName];
}
if ([curl GetVar: @"file_name"] != fileName) {
[curl ClearVar: @"file_id"];
[curl ClearVar: @"file_result_name"];
[curl ClearVar: @"file_parent_id"];
[curl ClearVar: @"file_search_count"];
[curl SetVar: @"file_name" varValue: fileName];
}
// With unchanged names, cached IDs and validation fields can be reused.
// Clear the corresponding cached fields to refresh after a remote rename/move.
NSString *localPath = @"qa_output/penguins.jpg";
// A cold plan is searchFolder -> searchFile -> targetCurl.
// No listing endpoint or manual search call is used.
CkoJsonObject *plan = [[CkoJsonObject alloc] init];
plan.EmitCompact = NO;
CkoStringBuilder *authorizedCommand1 = [[CkoStringBuilder alloc] init];
[authorizedCommand1 SetString: @"curl -X GET \"https://api.box.com/2.0/files/{{file_id}}/content\" -H \"Authorization: Bearer {{access_token}}\" -L"];
int headerReplacement1 = [[authorizedCommand1 Replace: @"{{access_token}}" replacement: [curl GetVar: @"access_token"]] intValue];
success = [curl ExaminePlan: [authorizedCommand1 GetAsString] json: plan];
if (success != YES) {
NSLog(@"%@",curl.LastErrorText);
return;
}
NSLog(@"%@",[plan Emit]);
// Keep the candidate download in memory; do not write an unvalidated result.
curl.ResponseFilePath = @"";
[curl ClearTargetOutput: @"*"];
CkoStringBuilder *authorizedCommand2 = [[CkoStringBuilder alloc] init];
[authorizedCommand2 SetString: @"curl -X GET \"https://api.box.com/2.0/files/{{file_id}}/content\" -H \"Authorization: Bearer {{access_token}}\" -L"];
int headerReplacement2 = [[authorizedCommand2 Replace: @"{{access_token}}" replacement: [curl GetVar: @"access_token"]] intValue];
success = [curl DoYourThing: [authorizedCommand2 GetAsString]];
if ((success != YES) || ([curl.StatusCode intValue] != 200)) {
NSLog(@"%@%d",@"Box request failed. HTTP status: ",[curl.StatusCode intValue]);
NSLog(@"%@%d",@"Failure reason: ",[curl.FailReason intValue]);
return;
}
// AddOutput2 supplies candidate IDs. Box's phrase search and recursive
// ancestor filter are not an exact path lookup, so validate before saving.
if (([curl GetVar: @"folder_search_count"] != @"1") || ([curl GetVar: @"file_search_count"] != @"1")) {
NSLog(@"%@",@"Search was ambiguous or incomplete. Expected exactly one result per search; no file was saved.");
NSLog(@"%@%@%@%@",@"Folder results: ",[curl GetVar: @"folder_search_count"],@" File results: ",[curl GetVar: @"file_search_count"]);
[curl ClearVar: @"folder_id"];
[curl ClearVar: @"folder_result_name"];
[curl ClearVar: @"folder_parent_id"];
[curl ClearVar: @"folder_search_count"];
[curl ClearVar: @"file_id"];
[curl ClearVar: @"file_result_name"];
[curl ClearVar: @"file_parent_id"];
[curl ClearVar: @"file_search_count"];
return;
}
if (([curl GetVar: @"folder_result_name"] != folderName) || ([curl GetVar: @"folder_parent_id"] != @"0") || ([curl GetVar: @"file_result_name"] != fileName) || ([curl GetVar: @"file_parent_id"] != [curl GetVar: @"folder_id"])) {
NSLog(@"%@",@"The search result is not the exact root-folder/filename requested; no file was saved.");
[curl ClearVar: @"folder_id"];
[curl ClearVar: @"folder_result_name"];
[curl ClearVar: @"folder_parent_id"];
[curl ClearVar: @"folder_search_count"];
[curl ClearVar: @"file_id"];
[curl ClearVar: @"file_result_name"];
[curl ClearVar: @"file_parent_id"];
[curl ClearVar: @"file_search_count"];
return;
}
// Validation is after the network download but before writing the destination.
// This in-memory example is intended for small files such as penguins.jpg.
// Create qa_output before running it.
CkoBinData *contents = [[CkoBinData alloc] init];
success = [curl GetResponseBd: contents];
if (success != YES) {
NSLog(@"%@",curl.LastErrorText);
return;
}
success = [contents WriteFile: localPath];
if (success != YES) {
NSLog(@"%@",contents.LastErrorText);
return;
}
NSLog(@"%@%@",@"Downloaded to ",localPath);