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

Delphi ActiveX Example

var
rest: TChilkatRest;
success: Integer;
bTls: Integer;
port: Integer;
bAutoReconnect: Integer;
fileStream: TChilkatStream;
strResponseBody: WideString;
respStatusCode: Integer;
jsonResponse: TChilkatJsonObject;
uploadToken: WideString;
uploadExpires_at: WideString;
uploadAttachmentUrl: WideString;
uploadAttachmentId: Integer;
uploadAttachmentFile_name: WideString;
uploadAttachmentContent_url: WideString;
uploadAttachmentMapped_content_url: WideString;
uploadAttachmentContent_type: WideString;
uploadAttachmentSize: Integer;
uploadAttachmentWidth: WideString;
uploadAttachmentHeight: WideString;
uploadAttachmentInline: Integer;
i: Integer;
count_i: Integer;
url: WideString;
id: Integer;
file_name: WideString;
content_url: WideString;
mapped_content_url: WideString;
content_type: WideString;
size: Integer;
width: WideString;
height: WideString;
inline: Integer;
j: Integer;
count_j: Integer;

begin
rest := TChilkatRest.Create(Self);

//  URL: https://subdomain.zendesk.com/api/v2/uploads.json?filename=sample.pdf
bTls := 1;
port := 443;
bAutoReconnect := 1;
success := rest.Connect('subdomain.zendesk.com',port,bTls,bAutoReconnect);
if (success <> 1) then
  begin
    Memo1.Lines.Add('ConnectFailReason: ' + IntToStr(rest.ConnectFailReason));
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;

rest.SetAuthBasic('zendesk_email','zendesk_password');

fileStream := TChilkatStream.Create(Self);
fileStream.SourceFile := 'sample.pdf';

rest.AddHeader('Content-Type','application/binary');

strResponseBody := rest.FullRequestStream('POST','/api/v2/uploads.json?filename=sample.pdf',fileStream.ControlInterface);
if (rest.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;
respStatusCode := rest.ResponseStatusCode;
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(rest.ResponseHeader);
    Memo1.Lines.Add('Response Body:');
    Memo1.Lines.Add(strResponseBody);
    Exit;
  end;

jsonResponse := TChilkatJsonObject.Create(Self);
jsonResponse.Load(strResponseBody);

uploadToken := jsonResponse.StringOf('upload.token');
uploadExpires_at := jsonResponse.StringOf('upload.expires_at');
uploadAttachmentUrl := jsonResponse.StringOf('upload.attachment.url');
uploadAttachmentId := jsonResponse.IntOf('upload.attachment.id');
uploadAttachmentFile_name := jsonResponse.StringOf('upload.attachment.file_name');
uploadAttachmentContent_url := jsonResponse.StringOf('upload.attachment.content_url');
uploadAttachmentMapped_content_url := jsonResponse.StringOf('upload.attachment.mapped_content_url');
uploadAttachmentContent_type := jsonResponse.StringOf('upload.attachment.content_type');
uploadAttachmentSize := jsonResponse.IntOf('upload.attachment.size');
uploadAttachmentWidth := jsonResponse.StringOf('upload.attachment.width');
uploadAttachmentHeight := jsonResponse.StringOf('upload.attachment.height');
uploadAttachmentInline := jsonResponse.BoolOf('upload.attachment.inline');
i := 0;
count_i := jsonResponse.SizeOfArray('upload.attachments');
while i < count_i do
  begin
    jsonResponse.I := i;
    url := jsonResponse.StringOf('upload.attachments[i].url');
    id := jsonResponse.IntOf('upload.attachments[i].id');
    file_name := jsonResponse.StringOf('upload.attachments[i].file_name');
    content_url := jsonResponse.StringOf('upload.attachments[i].content_url');
    mapped_content_url := jsonResponse.StringOf('upload.attachments[i].mapped_content_url');
    content_type := jsonResponse.StringOf('upload.attachments[i].content_type');
    size := jsonResponse.IntOf('upload.attachments[i].size');
    width := jsonResponse.StringOf('upload.attachments[i].width');
    height := jsonResponse.StringOf('upload.attachments[i].height');
    inline := jsonResponse.BoolOf('upload.attachments[i].inline');
    j := 0;
    count_j := jsonResponse.SizeOfArray('upload.attachments[i].thumbnails');
    while j < count_j do
      begin
        jsonResponse.J := j;
        j := j + 1;
      end;

    i := i + 1;
  end;

i := 0;
count_i := jsonResponse.SizeOfArray('upload.attachment.thumbnails');
while i < count_i do
  begin
    jsonResponse.I := i;
    i := i + 1;
  end;

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