Unicode C++ Box: Upload File

Back to Index

Uploads a file to a destination (parent) folder. The BOX_FOLDER_ID should be replaced with the id of the folder, or 0 for the root folder.

Documentation: https://developer.box.com/reference#upload-a-file

CURL Command

curl https://upload.box.com/api/2.0/files/content \
  -H "Authorization: Bearer BOX_ACCESS_TOKEN" -X POST \
  -F attributes='{"name":"tigers.jpeg", "parent":{"id":"BOX_FOLDER_ID"}}' \
  -F file=@tigers.jpeg

Unicode C++ Example

#include <CkRestW.h>
#include <CkStreamW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    CkRestW rest;
    bool success;

    //  URL: https://upload.box.com/api/2.0/files/content
    bool bTls = true;
    int port = 443;
    bool bAutoReconnect = true;
    success = rest.Connect(L"upload.box.com",port,bTls,bAutoReconnect);
    if (success != true) {
        wprintf(L"ConnectFailReason: %d\n",rest.get_ConnectFailReason());
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    rest.put_PartSelector(L"1");
    rest.AddHeader(L"Content-Disposition",L"form-data; name=\"attributes\"");
    rest.SetMultipartBodyString(L"{\"name\":\"tigers.jpeg\", \"parent\":{\"id\":\"BOX_FOLDER_ID\"}}");

    rest.put_PartSelector(L"2");
    CkStreamW fileStream2;
    fileStream2.put_SourceFile(L"tigers.jpeg");
    rest.AddHeader(L"Content-Disposition",L"form-data; name=\"tigers.jpeg\"; filename=\"tigers.jpeg\"");
    rest.AddHeader(L"Content-Type",L"image/jpeg");
    rest.SetMultipartBodyStream(fileStream2);

    rest.put_PartSelector(L"0");

    rest.AddHeader(L"Authorization",L"Bearer BOX_ACCESS_TOKEN");

    rest.put_PartSelector(L"0");
    rest.AddHeader(L"Content-Type",L"multipart/form-data");

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

    CkJsonObjectW jsonResponse;
    jsonResponse.Load(strResponseBody);

    int total_count;
    int i;
    int count_i;
    const wchar_t *type = 0;
    const wchar_t *id = 0;
    const wchar_t *sequence_id = 0;
    const wchar_t *etag = 0;
    const wchar_t *sha1 = 0;
    const wchar_t *name = 0;
    const wchar_t *description = 0;
    int size;
    int path_collectionTotal_count;
    const wchar_t *created_at = 0;
    const wchar_t *modified_at = 0;
    bool trashed_at;
    bool purged_at;
    const wchar_t *content_created_at = 0;
    const wchar_t *content_modified_at = 0;
    const wchar_t *created_byType = 0;
    const wchar_t *created_byId = 0;
    const wchar_t *created_byName = 0;
    const wchar_t *created_byLogin = 0;
    const wchar_t *modified_byType = 0;
    const wchar_t *modified_byId = 0;
    const wchar_t *modified_byName = 0;
    const wchar_t *modified_byLogin = 0;
    const wchar_t *owned_byType = 0;
    const wchar_t *owned_byId = 0;
    const wchar_t *owned_byName = 0;
    const wchar_t *owned_byLogin = 0;
    bool shared_link;
    const wchar_t *parentType = 0;
    const wchar_t *parentId = 0;
    const wchar_t *parentSequence_id = 0;
    const wchar_t *parentEtag = 0;
    const wchar_t *parentName = 0;
    const wchar_t *item_status = 0;
    int j;
    int count_j;
    bool sequence_id_bool;
    bool etag_bool;

    total_count = jsonResponse.IntOf(L"total_count");
    i = 0;
    count_i = jsonResponse.SizeOfArray(L"entries");
    while (i < count_i) {
        jsonResponse.put_I(i);
        type = jsonResponse.stringOf(L"entries[i].type");
        id = jsonResponse.stringOf(L"entries[i].id");
        sequence_id = jsonResponse.stringOf(L"entries[i].sequence_id");
        etag = jsonResponse.stringOf(L"entries[i].etag");
        sha1 = jsonResponse.stringOf(L"entries[i].sha1");
        name = jsonResponse.stringOf(L"entries[i].name");
        description = jsonResponse.stringOf(L"entries[i].description");
        size = jsonResponse.IntOf(L"entries[i].size");
        path_collectionTotal_count = jsonResponse.IntOf(L"entries[i].path_collection.total_count");
        created_at = jsonResponse.stringOf(L"entries[i].created_at");
        modified_at = jsonResponse.stringOf(L"entries[i].modified_at");
        trashed_at = jsonResponse.IsNullOf(L"entries[i].trashed_at");
        purged_at = jsonResponse.IsNullOf(L"entries[i].purged_at");
        content_created_at = jsonResponse.stringOf(L"entries[i].content_created_at");
        content_modified_at = jsonResponse.stringOf(L"entries[i].content_modified_at");
        created_byType = jsonResponse.stringOf(L"entries[i].created_by.type");
        created_byId = jsonResponse.stringOf(L"entries[i].created_by.id");
        created_byName = jsonResponse.stringOf(L"entries[i].created_by.name");
        created_byLogin = jsonResponse.stringOf(L"entries[i].created_by.login");
        modified_byType = jsonResponse.stringOf(L"entries[i].modified_by.type");
        modified_byId = jsonResponse.stringOf(L"entries[i].modified_by.id");
        modified_byName = jsonResponse.stringOf(L"entries[i].modified_by.name");
        modified_byLogin = jsonResponse.stringOf(L"entries[i].modified_by.login");
        owned_byType = jsonResponse.stringOf(L"entries[i].owned_by.type");
        owned_byId = jsonResponse.stringOf(L"entries[i].owned_by.id");
        owned_byName = jsonResponse.stringOf(L"entries[i].owned_by.name");
        owned_byLogin = jsonResponse.stringOf(L"entries[i].owned_by.login");
        shared_link = jsonResponse.IsNullOf(L"entries[i].shared_link");
        parentType = jsonResponse.stringOf(L"entries[i].parent.type");
        parentId = jsonResponse.stringOf(L"entries[i].parent.id");
        parentSequence_id = jsonResponse.stringOf(L"entries[i].parent.sequence_id");
        parentEtag = jsonResponse.stringOf(L"entries[i].parent.etag");
        parentName = jsonResponse.stringOf(L"entries[i].parent.name");
        item_status = jsonResponse.stringOf(L"entries[i].item_status");
        j = 0;
        count_j = jsonResponse.SizeOfArray(L"entries[i].path_collection.entries");
        while (j < count_j) {
            jsonResponse.put_J(j);
            type = jsonResponse.stringOf(L"entries[i].path_collection.entries[j].type");
            id = jsonResponse.stringOf(L"entries[i].path_collection.entries[j].id");
            sequence_id_bool = jsonResponse.IsNullOf(L"entries[i].path_collection.entries[j].sequence_id");
            etag_bool = jsonResponse.IsNullOf(L"entries[i].path_collection.entries[j].etag");
            name = jsonResponse.stringOf(L"entries[i].path_collection.entries[j].name");
            j = j + 1;
        }

        i = i + 1;
    }
    }

Sample JSON Response Body

{
  "total_count": 1,
  "entries": [
    {
      "type": "file",
      "id": "5000948880",
      "sequence_id": "3",
      "etag": "3",
      "sha1": "134b65991ed521fcfe4724b7d814ab8ded5185dc",
      "name": "tigers.jpeg",
      "description": "a picture of tigers",
      "size": 629644,
      "path_collection": {
        "total_count": 2,
        "entries": [
          {
            "type": "folder",
            "id": "0",
            "sequence_id": null,
            "etag": null,
            "name": "All Files"
          },
          {
            "type": "folder",
            "id": "11446498",
            "sequence_id": "1",
            "etag": "1",
            "name": "Pictures"
          }
        ]
      },
      "created_at": "2012-12-12T10:55:30-08:00",
      "modified_at": "2012-12-12T11:04:26-08:00",
      "trashed_at": null,
      "purged_at": null,
      "content_created_at": "2013-02-04T16:57:52-08:00",
      "content_modified_at": "2013-02-04T16:57:52-08:00",
      "created_by": {
        "type": "user",
        "id": "17738362",
        "name": "sean rose",
        "login": "sean@box.com"
      },
      "modified_by": {
        "type": "user",
        "id": "17738362",
        "name": "sean rose",
        "login": "sean@box.com"
      },
      "owned_by": {
        "type": "user",
        "id": "17738362",
        "name": "sean rose",
        "login": "sean@box.com"
      },
      "shared_link": null,
      "parent": {
        "type": "folder",
        "id": "11446498",
        "sequence_id": "1",
        "etag": "1",
        "name": "Pictures"
      },
      "item_status": "active"
    }
  ]
}