pythonpython-3.xgithub-apigithub-api-v3

receiving error 'Problems parsing JSON' when trying to comment on a pull request Using GitHub API


Im trying to comment on a pull request using githubs api but I'm getting the error shown in the title. The only solutions that I've seen on stack overflow is to json.dumps the data but that did not resolve the problem for me. What could I be doing wrong?

Here is my code

    def _pr_comment(self, res, pr_id):
        # POST / repos /: owner /:repo / issues /: issue_number / comments

        payload = {"body": json.dumps(res)}

        header = {'Authorization': 'token TOKEN',
                  "Accept": "application/vnd.github.+json"}

        response_decoded_json = requests.post(
            f'https://api.github.com/repos/REPO/Database-System/issues/{pr_id}/comments',
            data=payload, headers=header)

        response_json = response_decoded_json.json()

        print(response_json, response_decoded_json.status_code)

This is the full response that I receive along side the response code

{'message': 'Problems parsing JSON', 'documentation_url': 'https://developer.github.com/v3/issues/comments/#create-a-comment'} 400

Any help on this would be appreciated!


Solution

  • The error you're seeing relates to the payload which was submitted.

    Assuming res contains a string which is the actual body of the comment, I think you need to run the whole payoad through json.dumps then provide this as the data param to requests.post. So change it to something like:

    payload = json.dumps({"body": res})
    

    The requests documentation actually contains an example, related to the github API.