“记录一下对同一个接口有两种不同的返回结果的判断方法。”
签到成功返回的结果:
{
"result": 1
"exp": 5
"level_exp": {
"exp": 4770
"next_level_exp": 6000
"level": 12
"level_exp": 4500
}
}
已签到后再次请求签到接口返回的结果:
{
"error_code": -1,
"error": "今日已签到"
}
完整代码:
# -*- coding: utf-8 -*-
import requests
def sign():
url = 'https://api.xxxxxx.com/xxxx/signin'
headers = {}
data = {
"access_token": "5a40d710f301980bf1e735fxxxxxxxxxxx",
"userid": "5513a364fbe78e0xxxxxxxx"
}
r = requests.post(url=url, headers=headers, data=data)
req_result = r.json()
if "result" in req_result:
result = req_result["result"]
assert 1, result
print("签到成功")
elif "error_code" in req_result:
error_code = req_result["error_code"]
assert -1, error_code
print("今日已签到")
else:
print("签到请求失败")