【发布时间】:2021-05-19 10:05:18
【问题描述】:
请求方法:PATCH
有一个查询字符串参数部分
【问题讨论】:
-
@furus 你能检查一下吗?
-
@8349697 我试过了,但我的问题并没有解决
标签: python api python-requests patch
请求方法:PATCH
有一个查询字符串参数部分
【问题讨论】:
标签: python api python-requests patch
无法运行您的代码来重现您的错误。在此处查看response 所获得的内容:
r = requests.patch(url, headers=self._construct_header(),data=body)
response = getattr(r,'_content').decode("utf-8")
response_json = json.loads(response)
如果您将无效的 json 传递给 json.loads(),则会出现错误并显示类似消息。
import json
response = b'test data'.decode("utf-8")
print(response)
response_json = json.loads(response)
print(response_json)
输出:
test data
Traceback (most recent call last):
...
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
编辑:
在您的情况下,为避免错误,您需要添加一个 if-else 块。收到回复后,您需要检查您收到的具体内容。
r = requests.patch(url, headers=self._construct_header(),data=body)
# if necessary, check content type
print(r.headers['Content-Type'])
response = getattr(r,'_content').decode("utf-8")
if r.status_code == requests.codes.ok:
# make sure you get the string "success"
# if necessary, do something with the string
return response
else:
# if necessary, check what error you have: client or server errors, etc.
# or throw an exception to indicate that something went wrong
# if necessary, make sure you get the error in json format
# you may also get an error if the json is not valid
# since your api returns json formatted error message:
response_dict = json.loads(response)
return response_dict
在这些情况下,您的函数会返回字符串“success”或带有错误描述的 dict。
用法:
data = {
'correct_prediction': 'funny',
'is_accurate': 'False',
'newLabel': 'funny',
}
response = aiservice.update_prediction(data)
if isinstance(response, str):
print('New Prediction Status: ', response)
else:
# provide error information
# you can extract error description from dict
【讨论】: