小编stu*_*123的帖子

无法使用 Python3 模块请求 POST 到 Grafana

我正在尝试使用他们的后端 API 在 Grafana 上创建一个仪表板。我首先测试我的 API 令牌是通过使用 GET 设置的,并成功获得了 200 的返回码(如下所示)。然后我尝试使用 POST 创建一个简单的仪表板,但我一直收到 400 的返回代码。我很确定它与我尝试发送的有效负载有关,但我一直无法弄清楚. 这是我用于 JSON 格式的示例页面的链接。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)
Run Code Online (Sandbox Code Playgroud)

输出:

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

python-requests python-3.4 grafana

4
推荐指数
1
解决办法
2833
查看次数

标签 统计

grafana ×1

python-3.4 ×1

python-requests ×1