python-freshdesk icon indicating copy to clipboard operation
python-freshdesk copied to clipboard

Unable to add attachments to create_reply

Open dtolan opened this issue 3 years ago • 2 comments

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'}]

dtolan avatar Sep 30 '22 13:09 dtolan

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

dtolan avatar Sep 30 '22 13:09 dtolan

Thanks for the info. Would you like to create a PR?

sjkingo avatar Oct 01 '22 22:10 sjkingo