Sample code for 30+ languages & platforms
Unicode C++ Requires Chilkat v11.5.0+

Box: Upload a File from BinData

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. Resolve documents by name using HttpCurl, then upload a small local PDF from BinData with Rest. The transfer is explicit after dependency resolution; no ID is entered by the caller.

See the Box API guide for setup and the complete workflow.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpCurlW.h>
#include <CkStringBuilderW.h>
#include <CkBinDataW.h>
#include <CkRestW.h>
#include <CkOAuth2W.h>
#include <CkJsonObjectW.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";
    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;
        }

    }

    //  Resolve the named destination first. HttpCurl does not expand variables
    //  inside multipart -F attributes in the tested build, so this upload is two phases.
    curl.ClearTargetOutput(L"*");
    CkStringBuilderW authorizedCommand1;
    authorizedCommand1.SetString(L"curl -X GET \"https://api.box.com/2.0/folders/{{folder_id}}?fields=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() != 200)) {
        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;
    }

    CkBinDataW data;
    success = data.LoadFile(L"qa_data/pdf/invoice.pdf");
    if (success != true) {
        wprintf(L"%s\n",data.lastErrorText());
        return;
    }

    CkRestW rest;
    CkOAuth2W oauth2;
    oauth2.put_AccessToken(curl.getVar(L"access_token"));
    success = rest.SetAuthOAuth2(oauth2);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    success = rest.Connect(L"upload.box.com",443,true,true);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    CkJsonObjectW attributes;
    attributes.UpdateString(L"name",L"invoice.pdf");
    attributes.UpdateString(L"parent.id",curl.getVar(L"folder_id"));
    success = rest.AddHeader(L"Content-Type",L"multipart/form-data");
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    rest.put_PartSelector(L"1");
    success = rest.AddHeader(L"Content-Disposition",L"form-data; name=\"attributes\"");
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    success = rest.SetMultipartBodyString(attributes.emit());
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    rest.put_PartSelector(L"2");
    success = rest.AddHeader(L"Content-Disposition",L"form-data; name=\"file\"; filename=\"invoice.pdf\"");
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    success = rest.AddHeader(L"Content-Type",L"application/pdf");
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    success = rest.SetMultipartBodyBd(data);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    rest.put_PartSelector(L"0");
    const wchar_t *responseBody = rest.fullRequestMultipart(L"POST",L"/api/2.0/files/content");
    if (rest.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    if (rest.get_ResponseStatusCode() != 201) {
        wprintf(L"Upload failed: %d\n",rest.get_ResponseStatusCode());
        wprintf(L"%s\n",responseBody);
        return;
    }

    CkJsonObjectW result;
    success = result.Load(responseBody);
    if (success != true) {
        wprintf(L"%s\n",result.lastErrorText());
        return;
    }

    wprintf(L"Uploaded file ID: %s\n",result.stringOf(L"entries[0].id"));
    curl.SetVar(L"file_name",L"invoice.pdf");
    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.SetVar(L"file_id",result.stringOf(L"entries[0].id"));
    curl.ClearVar(L"file_version_id");
    curl.ClearVar(L"file_sha1");
    }