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

Unicode C Example

#include <C_CkRestW.h>
#include <C_CkStreamW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    HCkRestW rest;
    BOOL success;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkStreamW fileStream;
    const wchar_t *strResponseBody;
    int respStatusCode;
    HCkJsonObjectW jsonResponse;
    const wchar_t *uploadToken;
    const wchar_t *uploadExpires_at;
    const wchar_t *uploadAttachmentUrl;
    int uploadAttachmentId;
    const wchar_t *uploadAttachmentFile_name;
    const wchar_t *uploadAttachmentContent_url;
    const wchar_t *uploadAttachmentMapped_content_url;
    const wchar_t *uploadAttachmentContent_type;
    int uploadAttachmentSize;
    const wchar_t *uploadAttachmentWidth;
    const wchar_t *uploadAttachmentHeight;
    BOOL uploadAttachmentInline;
    int i;
    int count_i;
    const wchar_t *url;
    int id;
    const wchar_t *file_name;
    const wchar_t *content_url;
    const wchar_t *mapped_content_url;
    const wchar_t *content_type;
    int size;
    const wchar_t *width;
    const wchar_t *height;
    BOOL inline;
    int j;
    int count_j;

    rest = CkRestW_Create();

    //  URL: https://subdomain.zendesk.com/api/v2/uploads.json?filename=sample.pdf
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"subdomain.zendesk.com",port,bTls,bAutoReconnect);
    if (success != TRUE) {
        wprintf(L"ConnectFailReason: %d\n",CkRestW_getConnectFailReason(rest));
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        return;
    }

    CkRestW_SetAuthBasic(rest,L"zendesk_email",L"zendesk_password");

    fileStream = CkStreamW_Create();
    CkStreamW_putSourceFile(fileStream,L"sample.pdf");

    CkRestW_AddHeader(rest,L"Content-Type",L"application/binary");

    strResponseBody = CkRestW_fullRequestStream(rest,L"POST",L"/api/v2/uploads.json?filename=sample.pdf",fileStream);
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkStreamW_Dispose(fileStream);
        return;
    }

    respStatusCode = CkRestW_getResponseStatusCode(rest);
    if (respStatusCode >= 400) {
        wprintf(L"Response Status Code = %d\n",respStatusCode);
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkRestW_responseHeader(rest));
        wprintf(L"Response Body:\n");
        wprintf(L"%s\n",strResponseBody);
        CkRestW_Dispose(rest);
        CkStreamW_Dispose(fileStream);
        return;
    }

    jsonResponse = CkJsonObjectW_Create();
    CkJsonObjectW_Load(jsonResponse,strResponseBody);

    uploadToken = CkJsonObjectW_stringOf(jsonResponse,L"upload.token");
    uploadExpires_at = CkJsonObjectW_stringOf(jsonResponse,L"upload.expires_at");
    uploadAttachmentUrl = CkJsonObjectW_stringOf(jsonResponse,L"upload.attachment.url");
    uploadAttachmentId = CkJsonObjectW_IntOf(jsonResponse,L"upload.attachment.id");
    uploadAttachmentFile_name = CkJsonObjectW_stringOf(jsonResponse,L"upload.attachment.file_name");
    uploadAttachmentContent_url = CkJsonObjectW_stringOf(jsonResponse,L"upload.attachment.content_url");
    uploadAttachmentMapped_content_url = CkJsonObjectW_stringOf(jsonResponse,L"upload.attachment.mapped_content_url");
    uploadAttachmentContent_type = CkJsonObjectW_stringOf(jsonResponse,L"upload.attachment.content_type");
    uploadAttachmentSize = CkJsonObjectW_IntOf(jsonResponse,L"upload.attachment.size");
    uploadAttachmentWidth = CkJsonObjectW_stringOf(jsonResponse,L"upload.attachment.width");
    uploadAttachmentHeight = CkJsonObjectW_stringOf(jsonResponse,L"upload.attachment.height");
    uploadAttachmentInline = CkJsonObjectW_BoolOf(jsonResponse,L"upload.attachment.inline");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"upload.attachments");
    while (i < count_i) {
        CkJsonObjectW_putI(jsonResponse,i);
        url = CkJsonObjectW_stringOf(jsonResponse,L"upload.attachments[i].url");
        id = CkJsonObjectW_IntOf(jsonResponse,L"upload.attachments[i].id");
        file_name = CkJsonObjectW_stringOf(jsonResponse,L"upload.attachments[i].file_name");
        content_url = CkJsonObjectW_stringOf(jsonResponse,L"upload.attachments[i].content_url");
        mapped_content_url = CkJsonObjectW_stringOf(jsonResponse,L"upload.attachments[i].mapped_content_url");
        content_type = CkJsonObjectW_stringOf(jsonResponse,L"upload.attachments[i].content_type");
        size = CkJsonObjectW_IntOf(jsonResponse,L"upload.attachments[i].size");
        width = CkJsonObjectW_stringOf(jsonResponse,L"upload.attachments[i].width");
        height = CkJsonObjectW_stringOf(jsonResponse,L"upload.attachments[i].height");
        inline = CkJsonObjectW_BoolOf(jsonResponse,L"upload.attachments[i].inline");
        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(jsonResponse,L"upload.attachments[i].thumbnails");
        while (j < count_j) {
            CkJsonObjectW_putJ(jsonResponse,j);
            j = j + 1;
        }

        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"upload.attachment.thumbnails");
    while (i < count_i) {
        CkJsonObjectW_putI(jsonResponse,i);
        i = i + 1;
    }



    CkRestW_Dispose(rest);
    CkStreamW_Dispose(fileStream);
    CkJsonObjectW_Dispose(jsonResponse);

    }

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