How to submit user feedback using Sentry api in Python?

560 Views Asked by At

I've setup Sentry and got it automatically reporting exceptions, however I haven't managed to get user feedback submission working.

The python script is CLI based, so I would like the user to be able to type the bug information straight into the command line window, instead of generating a webpage and using that, as through the CLI is more seemless in my use case.

The code below was my attempt to follow the Sentry user feedback submission API Python documentation

The bearer_auth_token was obtained by following the Sentry auth API documentation

Example bare-bones code:

organisation_slug = 'cooldev'
project_slug = 'python-app'
bearer_auth_token = 'dcb275d6aaa040e1818326bec567cb0d9c09343171c65451ab53e740fa450314'  # with scope project:write
dsn_link = 'https://[email protected]/4423355'

import sentry_sdk, requests

sentry_sdk.init(
    dsn=dsn_link,
    traces_sample_rate=1.0,
    environment='testing'
)

def bug_send(event_id, name, email, comments):
    url = f'https://sentry.io/api/0/projects/{organisation_slug}/{project_slug}/user-feedback/'

    headers = {
        'Authorization': f'Bearer {bearer_auth_token}',
        'Content-Type': 'application/json'}

    data = {
        "event_id": str(event_id),
        "name": str(name),
        "email": str(email),
        "comments": str(comments)}

    response = requests.post(url, headers=headers, data=str(data))
    return response


try:
    x = 1 / 0
except Exception as e:
    event_id = sentry_sdk.last_event_id()
    name = input('Name: ')
    email = input('Email: ')
    comments = input('Comments: ')
    response = bug_send(event_id, name, email, comments)
    print(response.status_code, response.reason)
    input()

Output:

Name: John Doe
Email: [email protected]
Comments: It is not working!
400 Bad Request

2

There are 2 best solutions below

0
On

Removing the Content-Type header did the trick. The new response was 200 OK, and I could see the feedback shown up in Sentry

0
On

I hit the same confusing error (400 with no body) and realised after a while that the issue was caused by me using the slug from one project and the DSN of another.