PureBasic Box: Get Folder Items

Back to Index

Gets all of the files, folders, or web links contained within a folder.

Documentation: https://developer.box.com/reference#get-a-folders-items

CURL Command

curl https://api.box.com/2.0/folders/BOX_FOLDER_ID/items?limit=2&offset=0 \
-H "Authorization: Bearer BOX_ACCESS_TOKEN"

PureBasic Example

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

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

    success.i

    ;  URL: https://api.box.com/2.0/folders/BOX_FOLDER_ID/items?limit=2&offset=0
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"api.box.com",port,bTls,bAutoReconnect)
    If success <> 1
        Debug "ConnectFailReason: " + Str(CkRest::ckConnectFailReason(rest))
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

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

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

    success = CkRest::ckFullRequestNoBodySb(rest,"GET","/2.0/folders/BOX_FOLDER_ID/items?limit=2&offset=0",sbResponseBody)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

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

    CkJsonObject::ckLoadSb(jsonResponse,sbResponseBody)

    total_count.i
    offset.i
    limit.i
    i.i
    count_i.i
    type.s
    id.s
    file_versionType.s
    file_versionId.s
    file_versionSha1.s
    sequence_id.s
    etag.s
    sha1.s
    name.s
    by.s
    direction.s

    total_count = CkJsonObject::ckIntOf(jsonResponse,"total_count")
    offset = CkJsonObject::ckIntOf(jsonResponse,"offset")
    limit = CkJsonObject::ckIntOf(jsonResponse,"limit")
    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")
        file_versionType = CkJsonObject::ckStringOf(jsonResponse,"entries[i].file_version.type")
        file_versionId = CkJsonObject::ckStringOf(jsonResponse,"entries[i].file_version.id")
        file_versionSha1 = CkJsonObject::ckStringOf(jsonResponse,"entries[i].file_version.sha1")
        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")
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"order")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        by = CkJsonObject::ckStringOf(jsonResponse,"order[i].by")
        direction = CkJsonObject::ckStringOf(jsonResponse,"order[i].direction")
        i = i + 1
    Wend


    CkRest::ckDispose(rest)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jsonResponse)


    ProcedureReturn
EndProcedure

Sample JSON Response Body

{
  "total_count": 2,
  "entries": [
    {
      "type": "file",
      "id": "246181882790",
      "file_version": {
        "type": "file_version",
        "id": "259636211878",
        "sha1": "c9d2492fb97f88a9b4d1e35f32a3410e95853f18"
      },
      "sequence_id": "0",
      "etag": "0",
      "sha1": "c9d2492fb97f88a9b4d1e35f32a3410e95853f18",
      "name": "hedgehogs.jpg"
    },
    {
      "type": "file",
      "id": "246167973161",
      "file_version": {
        "type": "file_version",
        "id": "259621592361",
        "sha1": "df7be9dc4f467187783aca68c7ce98e4df2172d0"
      },
      "sequence_id": "0",
      "etag": "0",
      "sha1": "df7be9dc4f467187783aca68c7ce98e4df2172d0",
      "name": "penguins.jpg"
    }
  ],
  "offset": 0,
  "limit": 2,
  "order": [
    {
      "by": "type",
      "direction": "ASC"
    },
    {
      "by": "name",
      "direction": "ASC"
    }
  ]
}