Giter VIP home page Giter VIP logo

Comments (5)

seetharam-rajagopal avatar seetharam-rajagopal commented on September 13, 2024 3

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)

from fresh-samples.

rostagnolisandro avatar rostagnolisandro commented on September 13, 2024 1

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.

from fresh-samples.

atmishra avatar atmishra commented on September 13, 2024

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

from fresh-samples.

luckypur avatar luckypur commented on September 13, 2024

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

from fresh-samples.

rostagnolisandro avatar rostagnolisandro commented on September 13, 2024

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?

from fresh-samples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.