Upload Multiple Files (Uploads API) and Add as Attachment to Ticket Comment (Tickets API)
AnsweredHello,
I am currently developing an app that has 2 fields, a file upload field that allows multiple files and a text area.
Here is the current status of the app:
- I was able to create a private comment using the value inputted on the text area
- I was able to get the token when I uploaded the files using the Upload API
The next goal is for all the files to be attached to the ticket. I am using this as my reference and on the examples, only 1 file is attached to the ticket comment.
Can someone confirm if it is possible to attach multiple files/images to 1 ticket comment? If yes, can someone point me in the right direction?
Thank you,
Benessa
-
Hello,
As you can see on the documentation page you specify, in the json, attachments is an array.
Du can therefore specify several elements in this array:"attachments": [
{ "url": "https://example.zendesk.com/api/v2/attachments/1503194928902.json",
"id":1503194928902,
"file_name": "order_issue.png",
"content_url": "https://example.zendesk.com/attachments/token/vp7DnuiSvehLZtK2yrPjqJ1l6/?name=order_issue.png",
"content_type": "image/png"
},
{ "url": "https://example.zendesk.com/api/v2/attachments/987984664654.json",
"id":54687987987987,
"file_name": "order_issue2.png",
"content_url": "https://example.zendesk.com/attachments/token/dssqdqs65d4qd65f4/?name=order_issue2.png",
"content_type": "image/png"
}
]I admit not having had this need and therefore not tested, but I think it's a good way.
Serge.
-
Actually, I am passing multiple files to Uploads API but I think since the Content-type is application/binary it only allows 1 file to be uploaded. Thus, it only generates 1 token that I can use when I create a new comment using the Tickets API.
Do you have any idea or workaround on how can I attach more than 1 file to the new comment using the Uploads API?
Appreciate your response.
Thank you,
Benessa -
Hi Bennessa,
However, I manage to do this.
Below is a piece of my python code to upload multiple files in a single comment.You see that I first load an Array :
...
tab_token = []
headers = {'Content-type': 'application/binary'}
for cur_att in ATT_FILE:
filename = os.path.basename(cur_att)
while True:
r_attach = requests.post(AuthConfig.get('zendesk','url') + '/api/v2/uploads.json?filename=' + filename, data=open(cur_att, 'rb'), headers=headers, auth=(AuthConfig.get('auth','user') , AuthConfig.get('auth','pass')))
data_token = r_attach.json()
cur_token = data_token['upload']['token']
tab_token.append(cur_token)
...then that in my json ticket I specify my array in uploads.
data_ticket = {
'ticket': {
'subject': SUJET,
'comment': { 'html_body': COMMENTAIRE , 'public' : false , 'uploads': tab_token },
....In hope this will help you.
Friendships,
Serge. -
May I ask what is the correct data format that I need to pass on the data parameter to /api/v2/uploads.json to get the upload token?
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 and when it was uploaded and attached to the ticket I am getting the error Error loading image when I tried opening the attached file.
I also tried passing Binary and Base64 data but I am getting the error
{ "readyState": 4, "responseText": "{\"error\":\"AttachmentUnprocessable\",\"description\":\"Attachment file could not be processed.\"}", "status": 422, "statusText": "error" }
Appreciate your response.
Thank you,
-
Hello Serge BERTAINA DUBOIS,
Happy to inform you that I was able to make my code work.
Thank you for your help.
~ Benessa
Please sign in to leave a comment.
5 Comments