PureBasic Zendesk: Upload an Attachment File

Back to Index

Uploads an attachment file.

Documentation: https://developer.zendesk.com/rest_api/docs/core/attachments#upload-files

CURL Command

curl -X POST https://subdomain.zendesk.com/api/v2/uploads.json?filename=sample.pdf \
  -H "Content-Type: application/binary" \
  --data-binary @sample.pdf \
  -u zendesk_email:zendesk_password

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://subdomain.zendesk.com/api/v2/uploads.json?filename=sample.pdf
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"subdomain.zendesk.com",port,bTls,bAutoReconnect)
    If success <> 1
        Debug "ConnectFailReason: " + Str(CkRest::ckConnectFailReason(rest))
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    CkRest::ckSetAuthBasic(rest,"zendesk_email","zendesk_password")

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

    CkStream::setCkSourceFile(fileStream, "sample.pdf")

    CkRest::ckAddHeader(rest,"Content-Type","application/binary")

    strResponseBody.s = CkRest::ckFullRequestStream(rest,"POST","/api/v2/uploads.json?filename=sample.pdf",fileStream)
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkStream::ckDispose(fileStream)
        ProcedureReturn
    EndIf

    respStatusCode.i = CkRest::ckResponseStatusCode(rest)
    If respStatusCode >= 400
        Debug "Response Status Code = " + Str(respStatusCode)
        Debug "Response Header:"
        Debug CkRest::ckResponseHeader(rest)
        Debug "Response Body:"
        Debug strResponseBody
        CkRest::ckDispose(rest)
        CkStream::ckDispose(fileStream)
        ProcedureReturn
    EndIf

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

    CkJsonObject::ckLoad(jsonResponse,strResponseBody)

    uploadToken.s
    uploadExpires_at.s
    uploadAttachmentUrl.s
    uploadAttachmentId.i
    uploadAttachmentFile_name.s
    uploadAttachmentContent_url.s
    uploadAttachmentMapped_content_url.s
    uploadAttachmentContent_type.s
    uploadAttachmentSize.i
    uploadAttachmentWidth.s
    uploadAttachmentHeight.s
    uploadAttachmentInline.i
    i.i
    count_i.i
    url.s
    id.i
    file_name.s
    content_url.s
    mapped_content_url.s
    content_type.s
    size.i
    width.s
    height.s
    inline.i
    j.i
    count_j.i

    uploadToken = CkJsonObject::ckStringOf(jsonResponse,"upload.token")
    uploadExpires_at = CkJsonObject::ckStringOf(jsonResponse,"upload.expires_at")
    uploadAttachmentUrl = CkJsonObject::ckStringOf(jsonResponse,"upload.attachment.url")
    uploadAttachmentId = CkJsonObject::ckIntOf(jsonResponse,"upload.attachment.id")
    uploadAttachmentFile_name = CkJsonObject::ckStringOf(jsonResponse,"upload.attachment.file_name")
    uploadAttachmentContent_url = CkJsonObject::ckStringOf(jsonResponse,"upload.attachment.content_url")
    uploadAttachmentMapped_content_url = CkJsonObject::ckStringOf(jsonResponse,"upload.attachment.mapped_content_url")
    uploadAttachmentContent_type = CkJsonObject::ckStringOf(jsonResponse,"upload.attachment.content_type")
    uploadAttachmentSize = CkJsonObject::ckIntOf(jsonResponse,"upload.attachment.size")
    uploadAttachmentWidth = CkJsonObject::ckStringOf(jsonResponse,"upload.attachment.width")
    uploadAttachmentHeight = CkJsonObject::ckStringOf(jsonResponse,"upload.attachment.height")
    uploadAttachmentInline = CkJsonObject::ckBoolOf(jsonResponse,"upload.attachment.inline")
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"upload.attachments")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        url = CkJsonObject::ckStringOf(jsonResponse,"upload.attachments[i].url")
        id = CkJsonObject::ckIntOf(jsonResponse,"upload.attachments[i].id")
        file_name = CkJsonObject::ckStringOf(jsonResponse,"upload.attachments[i].file_name")
        content_url = CkJsonObject::ckStringOf(jsonResponse,"upload.attachments[i].content_url")
        mapped_content_url = CkJsonObject::ckStringOf(jsonResponse,"upload.attachments[i].mapped_content_url")
        content_type = CkJsonObject::ckStringOf(jsonResponse,"upload.attachments[i].content_type")
        size = CkJsonObject::ckIntOf(jsonResponse,"upload.attachments[i].size")
        width = CkJsonObject::ckStringOf(jsonResponse,"upload.attachments[i].width")
        height = CkJsonObject::ckStringOf(jsonResponse,"upload.attachments[i].height")
        inline = CkJsonObject::ckBoolOf(jsonResponse,"upload.attachments[i].inline")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(jsonResponse,"upload.attachments[i].thumbnails")
        While j < count_j
            CkJsonObject::setCkJ(jsonResponse, j)
            j = j + 1
        Wend
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"upload.attachment.thumbnails")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        i = i + 1
    Wend


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


    ProcedureReturn
EndProcedure

Sample JSON Response Body

{
  "upload": {
    "token": "W7Y11FCxfQlzOFPDbVDUEE45C",
    "expires_at": "2018-04-18T20:03:56Z",
    "attachments": [
      {
        "url": "https://chilkat.zendesk.com/api/v2/attachments/360887735934.json",
        "id": 360887735934,
        "file_name": "sample.pdf",
        "content_url": "https://chilkat.zendesk.com/attachments/token/Q7wAubpKB2KERnNFj89JmdvS1/?name=sample.pdf",
        "mapped_content_url": "https://chilkat.zendesk.com/attachments/token/Q7wAubpKB2KERnNFj89JmdvS1/?name=sample.pdf",
        "content_type": "application/pdf",
        "size": 178399,
        "width": null,
        "height": null,
        "inline": false,
        "thumbnails": [
        ]
      }
    ],
    "attachment": {
      "url": "https://chilkat.zendesk.com/api/v2/attachments/360887735934.json",
      "id": 360887735934,
      "file_name": "sample.pdf",
      "content_url": "https://chilkat.zendesk.com/attachments/token/Q7wAubpKB2KERnNFj89JmdvS1/?name=sample.pdf",
      "mapped_content_url": "https://chilkat.zendesk.com/attachments/token/Q7wAubpKB2KERnNFj89JmdvS1/?name=sample.pdf",
      "content_type": "application/pdf",
      "size": 178399,
      "width": null,
      "height": null,
      "inline": false,
      "thumbnails": [
      ]
    }
  }
}