I'm currently working on a script to update Grafana dashboards through the API, and I keep encountering issues when sending my JSON payload. Each time I make the request, I receive a <response 400> error, which seems to suggest a problem with the JSON formatting. I've taken care to ensure that my JSON payload follows the template provided in the API documentation, yet the problem persists. Could someone assist me in identifying what might be causing this issue? I'm relatively new to updating Grafana dashboards via API and not entirely sure what might be missing or incorrect in my approach.
I suspect that the issue might be related to how I've structured my JSON payload, but I'm not entirely sure what might be causing the problem, as I'm new to this process of updating Grafana dashboards programmatically. Any insights or suggestions would be greatly appreciated.
Here's the JSON payload I'm attempting to send: And here the function I'm using to make the POST request to Grafana:
{
"dashboard": {
"id": 765,
"panels": [
{
"alert": {
"alertRuleTags": {},
"conditions": [
{
"evaluator": {
"params": [
70,
5
],
"type": "within_range"
},
"operator": {
"type": "and"
},
"query": {
"params": [
"B",
"24h",
"now-12h"
]
},
"reducer": {
"params": [],
"type": "last"
},
"type": "query"
}
],
"executionErrorState": "keep_state",
"for": "5m",
"frequency": "1m",
"handler": 1,
"message": "",
"name": "XX",
"noDataState": "keep_state",
"notifications": []
},
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "1",
"fieldConfig": {
"defaults": {
"custom": {},
"links": []
},
"overrides": []
},
"fill": 1,
"fillGradient": 0,
"gridPos": {
"h": 9,
"w": 6,
"x": 0,
"y": 0
},
"hiddenSeries": false,
"id": 4,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"nullPointMode": "null",
"options": {
"alertThreshold": true
},
"percentage": false,
"pluginVersion": "7.3.5",
"pointradius": 2,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"alias": "",
"groupBy": [
{
"params": [
"1s"
],
"type": "time"
}
],
"hide": false,
"measurement": "",
"orderByTime": "ASC",
"policy": "default",
"refId": "A",
"resultFormat": "time_series",
"select": [
[
{
"params": [
""
],
"type": "field"
},
{
"params": [],
"type": "last"
}
]
],
"tags": [
{
"key": "",
"operator": "=",
"value": ""
},
{
"condition": "AND",
"key": "source",
"operator": "=",
"value": "file"
}
]
},
{
"alias": "",
"groupBy": [
{
"params": [
"1s"
],
"type": "time"
}
],
"hide": false,
"measurement": "telemetry",
"orderByTime": "ASC",
"policy": "default",
"refId": "B",
"resultFormat": "time_series",
"select": [
[
{
"params": [
""
],
"type": "field"
},
{
"params": [],
"type": "last"
}
]
],
"tags": [
{
"key": "",
"operator": "=",
"value": ""
},
{
"condition": "AND",
"key": "source",
"operator": "=",
"value": "file"
}
]
}
],
"thresholds": [
{
"colorMode": "critical",
"fill": true,
"line": true,
"op": "gt",
"value": 3,
"yaxis": "left"
},
{
"colorMode": "critical",
"fill": true,
"line": true,
"op": "lt",
"value": 5,
"yaxis": "left"
}
],
"timeFrom": null,
"timeRegions": [],
"timeShift": null,
"title": "",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"format": "volt",
"label": null,
"logBase": 1,
"max": "30",
"min": "0",
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
}
],
"uid": "o-XW-FgSk",
"version": 12
},
"overwrite": true,
"message": "Testing"
}
And here the function I'm using to make the POST request to Grafana:
def post_request_grafana(endpoint, data):
logging.basicConfig(level=logging.INFO)
auth_token = "redacted"
headers = {
"Authorization": f"Bearer {auth_token}",
"Content-Type": "application/json",
"Accept": "application/json"
}
try:
r = requests.post(endpoint, headers=headers, json=data)
r.raise_for_status()
logging.info(f"Successfully posted data to {endpoint}.")
return r.json()
except requests.exceptions.RequestException as err:
logging.error(f"Error posting to Grafana: {err}")
}