Upload Multiple Files (Uploads API) and Add as Attachment to Ticket Comment (Tickets API)

Répondu

5 Commentaires

  • Serge BERTAINA DUBOIS

    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.

    0
  • Benessa Dumol

    Hi Serge BERTAINA DUBOIS,

    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

    0
  • Serge BERTAINA DUBOIS

    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.

    0
  • Benessa Dumol

    Hi Serge BERTAINA DUBOIS,

    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,

    0
  • Benessa Dumol

    Hello Serge BERTAINA DUBOIS,

    Happy to inform you that I was able to make my code work.

    Thank you for your help.

    ~ Benessa

    0

Vous devez vous connecter pour laisser un commentaire.

Réalisé par Zendesk