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 <C_CkRestW.h>
#include <C_CkStreamW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    HCkRestW rest;
    BOOL success;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkStreamW fileStream2;
    const wchar_t *strResponseBody;
    HCkJsonObjectW jsonResponse;
    int total_count;
    int i;
    int count_i;
    const wchar_t *type;
    const wchar_t *id;
    const wchar_t *sequence_id;
    const wchar_t *etag;
    const wchar_t *sha1;
    const wchar_t *name;
    const wchar_t *description;
    int size;
    int path_collectionTotal_count;
    const wchar_t *created_at;
    const wchar_t *modified_at;
    BOOL trashed_at;
    BOOL purged_at;
    const wchar_t *content_created_at;
    const wchar_t *content_modified_at;
    const wchar_t *created_byType;
    const wchar_t *created_byId;
    const wchar_t *created_byName;
    const wchar_t *created_byLogin;
    const wchar_t *modified_byType;
    const wchar_t *modified_byId;
    const wchar_t *modified_byName;
    const wchar_t *modified_byLogin;
    const wchar_t *owned_byType;
    const wchar_t *owned_byId;
    const wchar_t *owned_byName;
    const wchar_t *owned_byLogin;
    BOOL shared_link;
    const wchar_t *parentType;
    const wchar_t *parentId;
    const wchar_t *parentSequence_id;
    const wchar_t *parentEtag;
    const wchar_t *parentName;
    const wchar_t *item_status;
    int j;
    int count_j;
    BOOL sequence_id_bool;
    BOOL etag_bool;

    rest = CkRestW_Create();

    //  URL: https://upload.box.com/api/2.0/files/content
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"upload.box.com",port,bTls,bAutoReconnect);
    if (success != TRUE) {
        wprintf(L"ConnectFailReason: %d\n",CkRestW_getConnectFailReason(rest));
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        return;
    }

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

    CkRestW_putPartSelector(rest,L"2");
    fileStream2 = CkStreamW_Create();
    CkStreamW_putSourceFile(fileStream2,L"tigers.jpeg");
    CkRestW_AddHeader(rest,L"Content-Disposition",L"form-data; name=\"tigers.jpeg\"; filename=\"tigers.jpeg\"");
    CkRestW_AddHeader(rest,L"Content-Type",L"image/jpeg");
    CkRestW_SetMultipartBodyStream(rest,fileStream2);

    CkRestW_putPartSelector(rest,L"0");

    CkRestW_AddHeader(rest,L"Authorization",L"Bearer BOX_ACCESS_TOKEN");

    CkRestW_putPartSelector(rest,L"0");
    CkRestW_AddHeader(rest,L"Content-Type",L"multipart/form-data");

    strResponseBody = CkRestW_fullRequestMultipart(rest,L"POST",L"/api/2.0/files/content");
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkStreamW_Dispose(fileStream2);
        return;
    }

    jsonResponse = CkJsonObjectW_Create();
    CkJsonObjectW_Load(jsonResponse,strResponseBody);

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

        i = i + 1;
    }



    CkRestW_Dispose(rest);
    CkStreamW_Dispose(fileStream2);
    CkJsonObjectW_Dispose(jsonResponse);

    }

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"
    }
  ]
}