Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
atmishra opened this issue Mar 9, 2017 · 5 comments

Comments

@atmishra
Copy link

atmishra commented Mar 9, 2017

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', ('', 'atul11@gmail.com')),
('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
Copy link
Author

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

@seetharam-rajagopal
Copy link
Contributor

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, 'example@example.com'),
  '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, 'sample_email@domain.com')
}

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)

@luckypur
Copy link

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

@rostagnolisandro
Copy link

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
Copy link

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': 'example@example.com',
        '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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants