PureBasic Box: Create Folder

Back to Index

Creates a new folder. To create a folder in the root folder, use a parent folder id = "0".

Documentation: https://developer.box.com/reference#create-a-new-folder

CURL Command

curl https://api.box.com/2.0/folders \
-H "Authorization: Bearer BOX_ACCESS_TOKEN" \
-d '{"name":"Important Docs", "parent": {"id": "BOX_FOLDER_ID"}}' \
-X POST

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
    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

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

    CkJsonObject::ckUpdateString(json,"name","Important Docs")
    CkJsonObject::ckUpdateString(json,"parent.id","BOX_FOLDER_ID")

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

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

    CkJsonObject::ckEmitSb(json,sbRequestBody)
    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestSb(rest,"POST","/2.0/folders",sbRequestBody,sbResponseBody)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

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

    CkJsonObject::ckLoadSb(jsonResponse,sbResponseBody)

    type.s
    id.s
    sequence_id.s
    etag.s
    name.s
    created_at.s
    modified_at.s
    description.s
    size.i
    path_collectionTotal_count.i
    created_byType.s
    created_byId.s
    created_byName.s
    created_byLogin.s
    modified_byType.s
    modified_byId.s
    modified_byName.s
    modified_byLogin.s
    trashed_at.i
    purged_at.i
    content_created_at.s
    content_modified_at.s
    owned_byType.s
    owned_byId.s
    owned_byName.s
    owned_byLogin.s
    shared_link.i
    folder_upload_email.i
    parentType.s
    parentId.s
    parentSequence_id.i
    parentEtag.i
    parentName.s
    item_status.s
    item_collectionTotal_count.i
    item_collectionOffset.i
    item_collectionLimit.i
    i.i
    count_i.i
    sequence_id_bool.i
    etag_bool.i
    by.s
    direction.s

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


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


    ProcedureReturn
EndProcedure

Sample JSON Response Body

{
  "type": "folder",
  "id": "47885473705",
  "sequence_id": "0",
  "etag": "0",
  "name": "Important Docs",
  "created_at": "2018-03-16T06:54:57-07:00",
  "modified_at": "2018-03-16T06:54:57-07:00",
  "description": "",
  "size": 0,
  "path_collection": {
    "total_count": 1,
    "entries": [
      {
        "type": "folder",
        "id": "0",
        "sequence_id": null,
        "etag": null,
        "name": "All Files"
      }
    ]
  },
  "created_by": {
    "type": "user",
    "id": "2787704945",
    "name": "chilkat",
    "login": "AutomationUser_434741_3nmGYSS7o5@@boxdevedition.com"
  },
  "modified_by": {
    "type": "user",
    "id": "2787704945",
    "name": "chilkat",
    "login": "AutomationUser_434741_3nmGYSS7o5@@boxdevedition.com"
  },
  "trashed_at": null,
  "purged_at": null,
  "content_created_at": "2018-03-16T06:54:57-07:00",
  "content_modified_at": "2018-03-16T06:54:57-07:00",
  "owned_by": {
    "type": "user",
    "id": "2787704945",
    "name": "chilkat",
    "login": "AutomationUser_434741_3nmGYSS7o5@@boxdevedition.com"
  },
  "shared_link": null,
  "folder_upload_email": null,
  "parent": {
    "type": "folder",
    "id": "0",
    "sequence_id": null,
    "etag": null,
    "name": "All Files"
  },
  "item_status": "active",
  "item_collection": {
    "total_count": 0,
    "entries": [
    ],
    "offset": 0,
    "limit": 100,
    "order": [
      {
        "by": "type",
        "direction": "ASC"
      },
      {
        "by": "name",
        "direction": "ASC"
      }
    ]
  }
}