0

any hel pwill be highly appreciated. Just can't make this to work. I am basically trying to update my Grafana dashboard via a http request. managed to get it to work with curl but want to do this with python's requests or pycurl.

curl -X PUT https://<api token>@ks.hostedgraphite.com/api/v2/grafana/dashboards/<my_dashboard> --data-binary @dashboard.json

The command above works. Tried several ways, an example of a code snippet:

crl = pycurl.Curl()
          crl.setopt(crl.URL,
           'https://[email protected]/api/v2/grafana/dashboards/<my_dashborad>')
crl.setopt(crl.UPLOAD, 1)
file = open('dashboard.json')
crl.setopt(crl.READDATA, file)
crl.perform()
crl.close()
file.close()
print('Status: {}'.format(crl.getinfo(crl.RESPONSE_CODE)))

2 Answers 2

0
curl -X PUT https://[email protected]/api/v2/grafana/dashboards/my_dashboard --data-binary @dashboard.json 

translates to

import requests

data = open('dashboard.json', 'rb').read()
response = requests.put('https://[email protected]/api/v2/grafana/dashboards/my_dashboard', data=data)

Just replace proper values for api_token and my_dashboard.

You can use https://curl.trillworks.com/ for converting curl commands to equivalent code using python requests.

1
  • Hi, thanks for answering. This gets a 401 response- "an api token is required", I think it doesn't recognize the token in the url. @pii_ke Commented May 22, 2020 at 3:33
0

ok, so my main issue was creating a http request via python requests. Figured it out, I setthe auth param to auth=("graphite_api_token","") and it worked:

data = json.dumps(dashbord_dict)
headers = {"Accept": "application/json","Content-Type":"application/json"}
response =requests.put('https://ks.hostedgraphite.com/api/v2/grafana/dashboards/some_dashboard,auth=("graphite_api_token",""),data=data,headers=headers)
            print(response.status_code)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.