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

Unable to add attachments to create_reply #81

Open
dtolan opened this issue Sep 30, 2022 · 2 comments
Open

Unable to add attachments to create_reply #81

dtolan opened this issue Sep 30, 2022 · 2 comments

Comments

@dtolan
Copy link

dtolan commented Sep 30, 2022

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

dtolan commented Sep 30, 2022

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

@sjkingo
Copy link
Owner

sjkingo commented Oct 1, 2022

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

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

2 participants