Recent searches


No recent searches

Ticket attachment file not working after download

Answered


Posted Aug 16, 2022

I'm using the attachment and ticket api for creating a Zendesk ticket with an attachment. The ticket gets created successfully and even generates a thumbnail for the attached file with the correct file name! But when I download and try to open the file, it cannot be opened and the file is only a few hundred bytes.

Here's the Ajax call for creating the attachment first:

{
    url: "https://{obscured}.zendesk.com/api/v2/uploads?filename="+file_name,
    type: "POST",
    headers: {
        "Authorization": "Basic "+wp_helper.zendesk_token,
        "Content-Type": file_mime,
    },
    data: JSON.stringify({
        "content_type": file_mime,
        "content_url": file_url,
        "file_name": file_name,
        "size": file_size,
    })
}

And here's the nested Ajax call for creating the ticket with the generated file token from the parent ajax call:

{
    url: "https://{obscured}.zendesk.com/api/v2/tickets",
    type: "POST",
    headers: {
        "Authorization": "Basic "+wp_helper.zendesk_token,
        "Content-Type": "application/json",
    },
    contentType: "application/json",
    data: JSON.stringify({
        "ticket": {
            "comment": {
                "body": comment,
                "uploads": [
                    file_token
                ]
            },
            "requester": { "locale_id": 1, "name": full_name, "email": email },
            "subject": subject
        }
    })
}

I've double checked all variables are correctly formatted according to documentation and everything looks good. The weirdest part is the ticket is created with an attachment with the correct file name, but the file somehow does not get created properly on Zendesk.

Any thoughts?


1

11

11 comments

Hi Luke!

Maybe you should use the following content type to upload the file:

'Content-type': 'application/binary'


friendships,
Serge.

0


Thanks for the reply Serge BERTAINA DUBOIS

Unfortunately that didn't work. :(

0


Luke,

 

Here is all my python code snippet on this attachment part:

    

      # GENERATION TOKENS ATTACHEMENT

      directory = ticket_dir + '/' + str(TicketID) # contain all my files

        tab_token = []

        for filename in os.listdir(directory):

          headers = {'Content-type': 'application/binary'}
          r_attach = requests.post(AuthConfig.get('zendesk','url') + '/api/v2/uploads.json?filename=' + filename, data=open(ticket_dir + '/' + str(TicketID) + '/' + filename, 'rb'), headers=headers, auth=(AuthConfig.get('auth','user') , AuthConfig.get('auth','pass'))) # Upload file
          data_token = r_attach.json()
          cur_token = data_token['upload']['token']
            tab_token.append(cur_token)

      # TICKET CREATION
      url_ticket = AuthConfig.get('zendesk','url') + "/api/v2/tickets.json"
      false = 'false'
        data_ticket = {'ticket': {'subject': SUJET, 'comment': { 'html_body': COMMENTAIRE ,  'public' : false , 'uploads':  tab_token }, 'group_id' : group_id , 'assignee_id' : assignee_id , 'requester_id' : requester_id , 'ticket_form_id' : AuthConfig.get('zendesk','FORM_ID') , 'type' : 'question' , 'priority' : 'normal' }}
       

Maybe something you find will help you.

Friendships,
Serge.

0


I'm at a loss here. The file continues to not work. Here's my latest ajax code for creating upload/attachment:

{
    url: "https://{obscured}.zendesk.com/api/v2/uploads?filename="+file_name,
    type: "POST",
    headers: {
        'Authorization': 'Basic '+wp_helper.zendesk_token,
        'Content-type': 'application/binary'
    },
    processData: false,
    data: file_base64
}

Note I'm passing the raw base64 content of the file.

0


image avatar

Eric Nelson

Zendesk Developer Advocacy

Hey Luke,

What kind of file are you trying to upload? 

0


Hey Eric Nelson,

I'm trying to upload a PDF file, but will need ability to upload docx, png etc. as well.

0


image avatar

Eric Nelson

Zendesk Developer Advocacy

Makes sense, just to confirm you're trying to do this via client-side javascript and not node.js?

0


That's right yep.. using AJAX to send POST request to Zendesk api. :)

0


image avatar

Eric Nelson

Zendesk Developer Advocacy

Here is an example using the fetch api, though the concept is the same. Make sure to set your Content-Type so that it matches the file type that you're uploading. 
 
 
 


async function uploadFile(data) {

const file = await fetch(data);
const content = await file.blob();
const request = await fetch("https://EXAMPLE.zendesk.com/api/v2/uploads.json?filename=example.pdf", {
method: 'POST', 
headers: {
'Content-Type': 'application/pdf',
'Authorization': 'Basic ABC123EXAMPLE'

}, body: content
});
let response = await request.json()
console.log(response)
}

1


Eric Nelson You rock! That worked perfectly. The thumbnails in Zendesk tickets now download a valid file that can be opened. We can close this ticket.

Really appreciate it!

0


image avatar

Eric Nelson

Zendesk Developer Advocacy

Hey Luke, 
 
Glad that worked for you, let us know if you need anything else.

0


Please sign in to leave a comment.

Didn't find what you're looking for?

New post