我正在尝试使用他们的后端 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) 当我在邮递员中发出此POST请求时,我得到了数据。当我在Python 2.7(使用Jupyter笔记本)中执行此操作时,出现错误“无法解码JSON对象”。我在做什么错,如何使它正常工作?
import json
import requests
url = 'http://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/BE0101A/BefolkningNy'
headers={'content-type': 'application/json'}
payload = {
"query": [
{
"code": "ContentsCode",
"selection": {
"filter": "item",
"values": [
"BE0101N1"
]
}
},
{
"code": "Tid",
"selection": {
"filter": "item",
"values": [
"2010",
"2011"
]
}
},
{
"code": "Region",
"selection": {
"filter": "item",
"values": [
"01"
]
}
}
],
"response": {
"format": "json"
}
}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=payload)
print(r.text)
print(r.json())
Run Code Online (Sandbox Code Playgroud)
api的手册在这里,但是并没有太大帮助:
http://www.scb.se/zh_/About-us/Open-data-API/API-for-the-Statistical-Database-/