python-freshdesk
python-freshdesk copied to clipboard
Unable to add attachments to create_reply
I am unable to create a new reply with an attachment. it is producing freshdesk.v2.errors.FreshdeskBadRequest: Validation failed: [{'field': 'attachments', 'message': 'It should contain elements of type valid file format only', 'code': 'datatype_mismatch'}]
This is a working solution that you can adjust as needed, I took the same approach as the creation of a new ticket
def create_reply(self, ticket_id, body, **kwargs):
url = "tickets/%d/reply" % ticket_id
data = {"body": body}
data.update(kwargs)
if 'attachments' in data:
attachments = data["attachments"]
del data["attachments"]
multipart_data = []
for attachment in attachments:
file_name = attachment.split("/")[-1:][0]
multipart_data.append(("attachments[]", (file_name, open(attachment, "rb"), None)))
comment = Comment(**self._api._post(url, data=data, files=multipart_data, headers={"Content-Type": None}))
else:
comment = Comment(**self._api._post(url, data=json.dumps(data)))
return comment
Thanks for the info. Would you like to create a PR?