fresh-samples icon indicating copy to clipboard operation
fresh-samples copied to clipboard

Create New ticket with custom fields and attachments using python requests.

Open atmishra opened this issue 7 years ago • 5 comments

I am using freshdesk API to create a new tickets with some custom fields and file attachments. Following the code sample given, the multipart structure which I am trying to send is:

multipart_data=[('subject', ('', 'sub')), ('status', ('', 3)), ('description', ('', 'des')), ('priority', ('', 2)), ('email', ('', '[email protected]')), ('custom_fields[]', ('info', 'reason: New Hire\nemployee: wmp\nuser: User One\npatch: value1\nteam: value1\ndescription: None\nattachment: None\nrequest_effected_date: None')), ('custom_fields[]', ('sub_category_name', 'I need to re-assign all accounts in a patch to another AE on my team')), ('custom_fields[]', ('category_name', 'Territory Assignment Change')), ('custom_fields[]', ('tenant_id', 'wild-butterfly-354')), ('custom_fields[]', ('category_id', 'asy8268aissugsudgutsuaytdu2')), ('custom_fields[]', ('sub_category_id', 'sjkhdksdaakdh89274987')), ('attachments[]', ('requirements.txt', <_io.BufferedReader name='requirements.txt'>, 'text/plain'))]

The request code written is: headers = {'Content-type': 'multipart/form-data'} response = requests.post(url, auth=__AUTH, headers = headers, files=meta_data)

The error which I am getting is: fdata = fp.read() AttributeError: 'int' object has no attribute 'read'

This error is coming from requests. I think this is because we are passing all the fields in files parameter, which is wrong.

atmishra avatar Mar 09 '17 19:03 atmishra

Even the sample code which is given for creating ticket with attachment is not working.

atmishra avatar Mar 10 '17 06:03 atmishra

Hi Atul, Please refer to the following code.

## This script requires "requests": http://docs.python-requests.org/
## To install: pip install requests

import requests
import json

api_key = "YOUR_API_KEY"
domain = "YOUR_DOMAIN"
password = "x"

multipart_data = {
  'email': (None, '[email protected]'),
  'subject': (None, 'Ticket Title'),
  'description': (None, 'Ticket description.'),
  'attachments[]': ('bat.jpg', open('bat.jpg', 'rb'), 'image/jpeg'),
  'status': (None, '2'),
  'priority': (None, '2'),
  'custom_fields[sample_text]': (None, 'this is the text'),
  'cc_emails[]': (None, '[email protected]')
}

r = requests.post("https://"+ domain +".freshdesk.com/api/v2/tickets", auth = (api_key, password), files = multipart_data)

if r.status_code == 201:
  print "Ticket created successfully, the response is given below" + r.content
  print "Location Header : " + r.headers['Location']
  print "x-request-id : " + r.headers['x-request-id']
else:
  print "Failed to create ticket, errors are displayed below,"
  response = json.loads(r.content)
  print response

  print "x-request-id : " + r.headers['x-request-id']
  print "Status Code : " + str(r.status_code)

seetharam-rajagopal avatar Mar 10 '17 12:03 seetharam-rajagopal

And how do i send multiple attachments...with attachment url not stream

luckypur avatar Apr 21 '17 07:04 luckypur

Hi @seetharam-rajagopal your code works great, thanks for sharing. However, I can't figure out how to adapt it for multiple attachments. Giving that multipart_data is a dictionary, 'attachments[]' key can be present only once. I've tried to set the value for that key to a list, but it doesn't work.

How would you do it for multiple attachments?

rostagnolisandro avatar Oct 20 '17 12:10 rostagnolisandro

Nevermind, I figured out. If you need to send multiple attachments, then you have to send the ticket field values inside the "data" argument in the post, and only use the "files" argument to send the multiple attachments:

requests.post(
    post_url, 
    auth=post_auth, 
    data={
        'subject': 'sub',
        'description': 'desc',
        'email': '[email protected]',
        'priority': 2,
        'status': 2
    },
    files=[
        ('attachments[]', ('photo.png', open('photo.png', 'rb'))),
        ('attachments[]', ('textfile.txt', open('textfile.txt', 'rb'))),
        ('attachments[]', ('compressed.zip', open('compressed.zip', 'rb'))),
    ]
)

It works like that.

rostagnolisandro avatar Oct 20 '17 13:10 rostagnolisandro