Zendesk API - Upload enpoint can't process file from file input
I'm working on a custom Zendesk App, written in React. My goal is to upload a file the user picks via an HTML input of type "file" (through javascript). The endpoint I'm trying to use is this one: https://developer.zendesk.com/api-reference/ticketing/tickets/ticket-attachments/#upload-files. I'm using the Zendesk client (which works for the other endpoints for me). My current code looks like this:
<input
type={"file"}
name={"file"}
id={"file"}
className={"hidden"}
ref={fileInput}
onChange={(e) => handleUpload(zdClient, e.target.files[0])}
/>
<Button onClick={() => fileInput.current.click()}>
<MD>Upload new</MD>
</Button>
export const handleUpload = async (zdClient, file) => {
if (!file) return;
const options = {
type: "POST",
url: `/api/v2/uploads.json?filename=${file?.name}`,
contentType: "application/binary",
data: file,
};
try {
const response = await sendClientRequest(zdClient, options);
console.log(response);
} catch (e) {
console.error(e);
}
};
export const sendClientRequest = async (client, options) => {
try {
return await client.request(options);
} catch (e) {
console.error(e);
}
};
The file object is there and it contains the correct parameters (like filename).
The response I get is the following:
{
"readyState": 4,
"responseText": "{\"error\":\"AttachmentUnprocessable\",\"description\":\"Attachment file could not be processed.\"}",
"status": 422,
"statusText": "error"
}
I have tried parsing the file object to base64 and used other FileReader methods on it, but I can't seem to get it to work. Any help and advice would be appreciated, many thanks.
-
Hi Ulas,
Which file types are you receiving this upload error for? Also, does the file extension for thefilename
URL parameter match the file extension of the local file?
Best,
-
Hey Christopher,
thank you for your comment. I've tried several data types like PDF or JPEG files and it happens for all of them. And yes the file extension matches the local file.
All the best,
-
As an update, I think this is caused by the ZAF Client not supporting operation with binary files.
-
Hi Ulas,
That can definitely result in an upload error. Are you able to successfully upload via AJAX instead of ZAFClient?
Best,
-
https://developer.zendesk.com/filmorago/api-reference/ticketing/tickets/ticket-attachments/#upload-files
This endpoint can be used only with curl?
Sorry if it's a stupid question -
Hi John,
All of our endpoints are language agnostic. For this endpoint just keep in mind that the payload needs to be sent in binary and must be a POST request. Other than that, feel free to communicate with it however works best for your use case.
Thanks!
-
Hi Eric Nelson and Christopher Kennedy,
I would like to ask both of you for help because I am getting the same issue.
It seems that I am not passing the correct data on the /api/v2/uploads.json?filename=attachment.jpg API endpoint. Currently, I am passing the filename with contentType: "image/jpg", and when it was uploaded and attached to the ticket I am getting the error Error loading image when I tried opening
I also tried passing Binary and Base64 data with contentType: "application/binary" but I am getting this error
{ "readyState": 4, "responseText": "{\"error\":\"AttachmentUnprocessable\",\"description\":\"Attachment file could not be processed.\"}", "status": 422, "statusText": "error" }
May I ask what is the correct data format that I need to pass on the data parameter?
Appreciate your response.
Thank you,
Benessa -
Hey Benessa,
I've gone ahead and replied in the ongoing thread relating to the same issue. You can view that here.
Thanks!
Tipene
Please sign in to leave a comment.
8 Comments