无法使用Python3模块请求发布到Grafana [英] Unable to POST to Grafana using Python3 module requests

查看:257
本文介绍了无法使用Python3模块请求发布到Grafana的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用其后端API在Grafana上创建仪表板.我首先测试通过使用GET设置了我的API令牌,并成功获取了200的返回码(如下所示).然后,我尝试使用POST创建一个简单的仪表板,但始终得到400的返回码.我很确定它与我要发送的有效负载有关,但是我一直无法弄清楚.这是指向我用于其JSON格式的示例页面的链接. http://docs.grafana.org/reference/http_api/

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 then try to use POST to create a simple dashboard but I keep getting a return code of 400. I'm pretty sure it has something to do with the payload I'm trying to send, but I have been unable to figure it out. Here is the link to the example page I'm using for their JSON format. http://docs.grafana.org/reference/http_api/

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": "%s" % dashboard}
url = "http://www.localhost/api/dashboards/db"

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

输出:

200
<Response [400]>
400
[{"classification":"DeserializationError","message":"invalid character 'd' looking for beginning of value"},{"fieldNames":["Dashboard"],"classification":"RequiredError","message":"Required"}]

推荐答案

问题是您的对象不是实际的json对象.

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

您可以对json = YOUR_PYTHON_OBJECT使用post方法

You can use post method with json=YOUR_PYTHON_OBJECT

因此,要修复代码,请将字典更改为仅使用常规的python字典,使用json = payload而不是data = payload.

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)

请注意仪表板中的差异,例如,行"从"[{}]"更改为"[{}]",因此它是python对象(带有空字典的列表),而不是字符串.

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.

输出为

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

这篇关于无法使用Python3模块请求发布到Grafana的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆