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

C Example

#include <C_CkRest.h>
#include <C_CkStream.h>
#include <C_CkJsonObject.h>

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

    rest = CkRest_Create();

    //  URL: https://upload.box.com/api/2.0/files/content
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRest_Connect(rest,"upload.box.com",port,bTls,bAutoReconnect);
    if (success != TRUE) {
        printf("ConnectFailReason: %d\n",CkRest_getConnectFailReason(rest));
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        return;
    }

    CkRest_putPartSelector(rest,"1");
    CkRest_AddHeader(rest,"Content-Disposition","form-data; name=\"attributes\"");
    CkRest_SetMultipartBodyString(rest,"{\"name\":\"tigers.jpeg\", \"parent\":{\"id\":\"BOX_FOLDER_ID\"}}");

    CkRest_putPartSelector(rest,"2");
    fileStream2 = CkStream_Create();
    CkStream_putSourceFile(fileStream2,"tigers.jpeg");
    CkRest_AddHeader(rest,"Content-Disposition","form-data; name=\"tigers.jpeg\"; filename=\"tigers.jpeg\"");
    CkRest_AddHeader(rest,"Content-Type","image/jpeg");
    CkRest_SetMultipartBodyStream(rest,fileStream2);

    CkRest_putPartSelector(rest,"0");

    CkRest_AddHeader(rest,"Authorization","Bearer BOX_ACCESS_TOKEN");

    CkRest_putPartSelector(rest,"0");
    CkRest_AddHeader(rest,"Content-Type","multipart/form-data");

    strResponseBody = CkRest_fullRequestMultipart(rest,"POST","/api/2.0/files/content");
    if (CkRest_getLastMethodSuccess(rest) != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkStream_Dispose(fileStream2);
        return;
    }

    jsonResponse = CkJsonObject_Create();
    CkJsonObject_Load(jsonResponse,strResponseBody);

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

        i = i + 1;
    }



    CkRest_Dispose(rest);
    CkStream_Dispose(fileStream2);
    CkJsonObject_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"
    }
  ]
}