PureBasic 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

PureBasic Example

IncludeFile "CkStream.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    ;  URL: https://upload.box.com/api/2.0/files/content
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"upload.box.com",port,bTls,bAutoReconnect)
    If success <> 1
        Debug "ConnectFailReason: " + Str(CkRest::ckConnectFailReason(rest))
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    CkRest::setCkPartSelector(rest, "1")
    CkRest::ckAddHeader(rest,"Content-Disposition","form-data; name=" + Chr(34) + "attributes" + Chr(34))
    CkRest::ckSetMultipartBodyString(rest,"{" + Chr(34) + "name" + Chr(34) + ":" + Chr(34) + "tigers.jpeg" + Chr(34) + ", " + Chr(34) + "parent" + Chr(34) + ":{" + Chr(34) + "id" + Chr(34) + ":" + Chr(34) + "BOX_FOLDER_ID" + Chr(34) + "}}")

    CkRest::setCkPartSelector(rest, "2")
    fileStream2.i = CkStream::ckCreate()
    If fileStream2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStream::setCkSourceFile(fileStream2, "tigers.jpeg")
    CkRest::ckAddHeader(rest,"Content-Disposition","form-data; name=" + Chr(34) + "tigers.jpeg" + Chr(34) + "; filename=" + Chr(34) + "tigers.jpeg" + Chr(34))
    CkRest::ckAddHeader(rest,"Content-Type","image/jpeg")
    CkRest::ckSetMultipartBodyStream(rest,fileStream2)

    CkRest::setCkPartSelector(rest, "0")

    CkRest::ckAddHeader(rest,"Authorization","Bearer BOX_ACCESS_TOKEN")

    CkRest::setCkPartSelector(rest, "0")
    CkRest::ckAddHeader(rest,"Content-Type","multipart/form-data")

    strResponseBody.s = CkRest::ckFullRequestMultipart(rest,"POST","/api/2.0/files/content")
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkStream::ckDispose(fileStream2)
        ProcedureReturn
    EndIf

    jsonResponse.i = CkJsonObject::ckCreate()
    If jsonResponse.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(jsonResponse,strResponseBody)

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

    total_count = CkJsonObject::ckIntOf(jsonResponse,"total_count")
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"entries")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        type = CkJsonObject::ckStringOf(jsonResponse,"entries[i].type")
        id = CkJsonObject::ckStringOf(jsonResponse,"entries[i].id")
        sequence_id = CkJsonObject::ckStringOf(jsonResponse,"entries[i].sequence_id")
        etag = CkJsonObject::ckStringOf(jsonResponse,"entries[i].etag")
        sha1 = CkJsonObject::ckStringOf(jsonResponse,"entries[i].sha1")
        name = CkJsonObject::ckStringOf(jsonResponse,"entries[i].name")
        description = CkJsonObject::ckStringOf(jsonResponse,"entries[i].description")
        size = CkJsonObject::ckIntOf(jsonResponse,"entries[i].size")
        path_collectionTotal_count = CkJsonObject::ckIntOf(jsonResponse,"entries[i].path_collection.total_count")
        created_at = CkJsonObject::ckStringOf(jsonResponse,"entries[i].created_at")
        modified_at = CkJsonObject::ckStringOf(jsonResponse,"entries[i].modified_at")
        trashed_at = CkJsonObject::ckIsNullOf(jsonResponse,"entries[i].trashed_at")
        purged_at = CkJsonObject::ckIsNullOf(jsonResponse,"entries[i].purged_at")
        content_created_at = CkJsonObject::ckStringOf(jsonResponse,"entries[i].content_created_at")
        content_modified_at = CkJsonObject::ckStringOf(jsonResponse,"entries[i].content_modified_at")
        created_byType = CkJsonObject::ckStringOf(jsonResponse,"entries[i].created_by.type")
        created_byId = CkJsonObject::ckStringOf(jsonResponse,"entries[i].created_by.id")
        created_byName = CkJsonObject::ckStringOf(jsonResponse,"entries[i].created_by.name")
        created_byLogin = CkJsonObject::ckStringOf(jsonResponse,"entries[i].created_by.login")
        modified_byType = CkJsonObject::ckStringOf(jsonResponse,"entries[i].modified_by.type")
        modified_byId = CkJsonObject::ckStringOf(jsonResponse,"entries[i].modified_by.id")
        modified_byName = CkJsonObject::ckStringOf(jsonResponse,"entries[i].modified_by.name")
        modified_byLogin = CkJsonObject::ckStringOf(jsonResponse,"entries[i].modified_by.login")
        owned_byType = CkJsonObject::ckStringOf(jsonResponse,"entries[i].owned_by.type")
        owned_byId = CkJsonObject::ckStringOf(jsonResponse,"entries[i].owned_by.id")
        owned_byName = CkJsonObject::ckStringOf(jsonResponse,"entries[i].owned_by.name")
        owned_byLogin = CkJsonObject::ckStringOf(jsonResponse,"entries[i].owned_by.login")
        shared_link = CkJsonObject::ckIsNullOf(jsonResponse,"entries[i].shared_link")
        parentType = CkJsonObject::ckStringOf(jsonResponse,"entries[i].parent.type")
        parentId = CkJsonObject::ckStringOf(jsonResponse,"entries[i].parent.id")
        parentSequence_id = CkJsonObject::ckStringOf(jsonResponse,"entries[i].parent.sequence_id")
        parentEtag = CkJsonObject::ckStringOf(jsonResponse,"entries[i].parent.etag")
        parentName = CkJsonObject::ckStringOf(jsonResponse,"entries[i].parent.name")
        item_status = CkJsonObject::ckStringOf(jsonResponse,"entries[i].item_status")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(jsonResponse,"entries[i].path_collection.entries")
        While j < count_j
            CkJsonObject::setCkJ(jsonResponse, j)
            type = CkJsonObject::ckStringOf(jsonResponse,"entries[i].path_collection.entries[j].type")
            id = CkJsonObject::ckStringOf(jsonResponse,"entries[i].path_collection.entries[j].id")
            sequence_id_bool = CkJsonObject::ckIsNullOf(jsonResponse,"entries[i].path_collection.entries[j].sequence_id")
            etag_bool = CkJsonObject::ckIsNullOf(jsonResponse,"entries[i].path_collection.entries[j].etag")
            name = CkJsonObject::ckStringOf(jsonResponse,"entries[i].path_collection.entries[j].name")
            j = j + 1
        Wend
        i = i + 1
    Wend


    CkRest::ckDispose(rest)
    CkStream::ckDispose(fileStream2)
    CkJsonObject::ckDispose(jsonResponse)


    ProcedureReturn
EndProcedure

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