我试图简单地调用化合物。金融API "https://api.compound.finance/api/v2/account"使用参数max_health。医生说
"如有,应以{"value"; "…字符串格式化数量……"}"。
(https://compound.finance/docs/api #代理记账)
所以我尝试了以下4种方法:
response = requests.get(
'https://api.compound.finance/api/v2/account',
params={
"max_health": "1.0" # method 1
"max_health": {"value":"1.0"} # method 2
"max_health": json.dumps({"value":"1.0"}) # method 3
}
)
但是它不起作用,我得到
HTTPError: 500 Server Error: Internal Server Error for url:…
你知道我应该格式化它吗?
他们没有更新API文档。你应该发送一个POST请求并提供参数作为请求体。
import json
import requests
url = "https://api.compound.finance/api/v2/account"
data = {
"max_health": {"value": "1.0"}
}
response = requests.post(url, data=json.dumps(data)) # <Response [200]>
response = response.json() # {'accounts': ...}
<<h4>编辑笔记/em>问题是API期望原始JSON,所以我使用json.dumps
。
Artyom已经解释了他美丽的答案,确实他们的API文档不幸过时了。除了他的回答,我想补充的是,requests
库支持json
参数,接受从requests
版本2.4.2
开始的原始JSON参数。因此,不再需要data=json.dumps(params)
。
见下面的代码
api_base = "https://api.compound.finance/api/v2/account"
params = {'max_health': {'value':'0.95'},
'min_borrow_value_in_eth': { 'value': '0.002' },
'page_number':19,
}
response = requests.post(api_base, json=params).json()