'How to add description in Github api?

This is my code:-

try:
    #some code
except Exception as error:
    import os
    os.system("cls")
    print("An error occured while running the code. Submitting the error to Github....")
    from env.Scripts import token
    tok = token.token
    e = str(error)
    import requests
    import json
    headers = {"Authorization" : "token {}".format(tok)}
    data2 = {"title": "JARVIS Error Reporting System Reported An Error"}
    label = {"labels": "Error"}
    body = {"body": [e]}
    url = "https://api.github.com/repos/Hashah2311/JARVIS/issues"
    requests.post(url,data=json.dumps(data2),headers=headers)
    exit()

I want the error in the description of issues so how can i do that? Please Guide. thank you!



Solution 1:[1]

According to the documentation of the github api I would expect the following to work:

headers = {"Authorization" : "token {}".format(tok)}
data = {
  "title": "JARVIS Error Reporting System Reported An Error",
  "body": f"An error ocurred: {error!r}",
  "labels": ["Error"],
}
url = "https://api.github.com/repos/Hashah2311/JARVIS/issues"
requests.post(url, json= data, headers= headers)

To be clear I made the following changes:

  • You defined the fields github expects in different dictionaries, but only sent one off. That can simply be fixed by putting all required data into the same dict (the one named data).
  • str(error) usually only gives you the description of the error. With repr(error) or f"{error!r}" you also get info about the type of error. And using a format string gives you a bit more control about how the generated description should look like.
    You can further customize that string to your liking.
  • The requests library can take care of json serialization. Simply specify the json keyword parameter instead of data as you did it before.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jonas V