如何将此curl命令转换为pycurl或python请求?

时间:2020-05-21 17:54:40

标签: api curl python-requests grafana pycurl

任何人都会受到高度赞赏。只是无法使它正常工作。我基本上是想通过http请求更新我的Grafana仪表板。设法使其能够与curl配合使用,但希望通过python的请求或pycurl做到这一点。

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

上面的命令有效。尝试了几种方法,例如一个代码片段:

crl = pycurl.Curl()
          crl.setopt(crl.URL,
           'https://apitoken@ks.hostedgraphite.com/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 个答案:

答案 0 :(得分:0)

ORA-00001: unique constraint (...) violated

翻译成

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

只需替换import requests data = open('dashboard.json', 'rb').read() response = requests.put('https://api_token@ks.hostedgraphite.com/api/v2/grafana/dashboards/my_dashboard', data=data) api_token的适当值即可。

您可以使用https://curl.trillworks.com/使用python my_dashboard将curl命令转换为等效代码。

答案 1 :(得分:0)

好的,所以我的主要问题是通过python请求创建一个http请求。 弄清楚了,我将auth参数设置为auth =(“ graphite_api_token”,“”),它起作用了:

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)