Grafana Dashboard not getting deleted through Grafana Delete API from NodeJS code

135 Views Asked by At

I was able to delete Grafana dashboard through its delete API from Postman but when I try deleting it through NodeJS code, it is not working. I get status as 200 from the code but the response body which I am getting is different than the Postman one.

I am using this delete API of grafana - grafana_host_url/api/dashboards/uid/:uid

and my code function is like this -

function deleteGrafana(req) {
    return new Promise((resolve, reject) => {
        console.log("Inside Delete Grafana")
        var dashbord_api_url = req.grafana_host_url + process.env.GRAFANA_DASHBOARD_DELETE_API_URL + req.uid
        var api_token = "Bearer " + req.grafana_api_key
        console.log("URL:",dashbord_api_url)
        console.log("api token:",api_token)
        var callOptions = {
            'method': 'DELETE',
            'url': dashbord_api_url,
            'headers': {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': api_token
            }
        };
        request.get(callOptions, (error, response) => {
            if (error) {
                throw new Error(error)
            }
            console.log("deleteGrafana Response" + response.body);

            var result = {};
            result.status = response.statusCode
            console.log("Result Status code:",result.status)
            resolve(result)
        });
    });
}

My response when I run it, is something like this -

deleteGrafana Response{"meta":{"type":"db","canSave":true,"canEdit":true,"canAdmin":true,"canStar":true,"canDelete":true,"slug":"sampletest","url":"/d/najkl-oo/sampletest","expires":"0001-01-01T00:00:00Z","created":"2022-12-29T11:43:00Z","updated":"2022-12-29T11:43:01Z","updatedBy":"Anonymous","createdBy":"Anonymous","version":2,"hasAcl":false,"isFolder":false,"folderId":0,"folderUid":"","folderTitle":"General","folderUrl":"","provisioned":false,"provisionedExternalId":"","annotationsPermissions":{"dashboard":{"canAdd".....

Ideal response should be as per actual grafana response for this api as something like this -

{
"id": xx,
"message": "Dashboard deleted",
 "title": "sample test"}

What should I do to resolve this, so my delete functionality works properly? When I check the grafana instance and browse the folders, the dashboard is still there.

1

There are 1 best solutions below

0
On

Just debugged more and saw some other functions. Changing request.get in request.get(callOptions, (error, response) => { to request.delete in code worked and gave the desired response.