uvdesk / api-bundle

API Integration for UVdesk Community Helpdesk System.
MIT License
17 stars 15 forks source link

Unable to create ticket with attachment in Python #41

Closed redvox27 closed 1 year ago

redvox27 commented 1 year ago

I am unable to create a ticket with attachments in Python. data = { 'message': message, 'actAsType': 'customer', 'name': name, 'subject': subject, 'from':from_email, } if attachments: data['attachments'] = attachments

Above is the code i am using to fill my data object. This will go into the POST request to the api. response = requests.post(url, data=data, headers=self.headers)

The ticket is actually created, however, attachments are nowhere to be found. See response below: {'status': 'success', 'response': {'message': 'Success! Ticket has been created successfully.', 'ticketId': 35}}

In the attachments key is a base64 string. Is this correct? What format is to be expected?

redvox27 commented 1 year ago

I've tried mutiple methods: base64, bytes, list of bytes objects, list of base64 objects. All of which result in a ticker or reply without any attachments.

redvox27 commented 1 year ago

I've found a solution which i wanted to share.

When you send the request directly from Postman, you'll get the results you're looking for. But when you try it in Python, it fails to create the attachment. Here's what I did to solve the issue for me:

You'll need to convert the file to bytes. If you start from a base64 string like me, then you have to convert it to bytes first. base64_bytes = base64.b64decode(base_64_string)

If your base64 starts with the format for exmaple: data:image/png;base64, .....

You want to split the string and use the ladder part. base64_bytes = base64.b64decode(base_64_string.split(',')[-1])

The sauce that made it work for me was to add the following lines of code: from io import BytesIO buf = BytesIO(base64_bytes)``response = requests.post(url, data=data, headers=self.headers, files={'attachments': ('test.png', buf.getbuffer())})

uvdeskTicketCreateBase64Result