0

I am migrating a repo from Bitbucket to GitHub and I am using the GH API.

According to the documentation: https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#update-an-issue-comment

you can easily add a comment in text.

I don't see any way to add an image (I am not talking about a link to an image).

I asked chatgpt but I guess it hallucinated when given me this:

def add_image_comment_to_issue(self, issue_number, image_path):
        url = f'{self.base_url}/issues/{issue_number}/comments'
        with open(image_path, 'rb') as file:
            image_data = file.read()
        base64_image = base64.b64encode(image_data).decode('utf-8')
        comment_body = f'![Screenshot](data:image/png;base64,{base64_image})'

        data = {'body': comment_body}
        response = requests.post(url, headers=self.headers, json=data)
        if response.status_code == 201:
            return response.json()['id']
        else:
            return None

Since that code does not work (response 422).

How can I upload images to issues or comments of issues?

halfer
  • 19,824
  • 17
  • 99
  • 186
JFerro
  • 3,203
  • 7
  • 35
  • 88

0 Answers0