Skip to content Skip to sidebar Skip to footer

Unable To Post To Grafana Using Python3 Module Requests

I'm trying to create a dashboard on Grafana using their backend API. I first test that my API token is set up by using GET and successfully get a return code of 200(shown below). I

Solution 1:

The problem is that your object is not an actual json object.

You can use post method with json=YOUR_PYTHON_OBJECT

So to fix your code, change your dictionary to use just a regular python dictionary, use json=payload, rather than data=payload.

So refactoring your code, you will have:

import requests
headers = {"Accept": "application/json",
           "Content-Type": "application/json",
           "Authorization": "Bearer xxx"
           }

r = requests.get("http://www.localhost", headers=headers)
print(r.text)
print(r.status_code)

dashboard = {"id": None,
             "title": "API_dashboard_test",
             "tags": ["CL-5"],
             "timezone": "browser",
             "rows": [{}],
             "schemaVersion": 6,
             "version": 0
             }
payload = {"dashboard": dashboard}
url = "http://www.localhost/api/dashboards/db"

p = requests.post(url, headers=headers, json=payload)
print(p)
print(p.status_code)
print(p.text)

Note the differences in dashboard, for example, "rows" was changed from "[{}]" to just [{}] so that it is a python object (list with empty dictionary), rather than a string.

The output is

200
<Response [200]>
200
{"slug":"api_dashboard_test","status":"success","version":0}

Post a Comment for "Unable To Post To Grafana Using Python3 Module Requests"