编辑 api 的响应代码
Edit response codes for api's
我将以 ConvertApi 为例。如果我将其集成到我的 python 代码中,例如:
try:
#Calling API here
except Exception as e:
print(e)
并获取响应码
{
"Code": 4013,
"Message": "User credentials not set, secret or token must be passed."
}
有没有办法打印我自己的文本。据我所知,我可以做到这一点
try:
#Calling API here
except:
print('It didn\'t work')
但是如果我得到 400 Bad Request
,我想说“检查你的连接”,对于 500 Internal Server Error
等,我想说“文件无法转换”等等。任何指导将不胜感激。
您可以设置字典,在其中根据特定响应创建自定义消息:
errors = {'400 Bad Request': 'Check your connection'}
try:
#Calling API here
except Exception as e:
print(errors.get('e'))
根据文档,您似乎只使用了响应 header,因此没有充分利用响应 body 的更细粒度的消息。为了做到这一点,你可以遵循这样的事情,甚至可能避免处理异常或构建你自己的字典:
response = # Calling API here
if 'Message' in response:
print(response['Message'])
我将以 ConvertApi 为例。如果我将其集成到我的 python 代码中,例如:
try:
#Calling API here
except Exception as e:
print(e)
并获取响应码
{
"Code": 4013,
"Message": "User credentials not set, secret or token must be passed."
}
有没有办法打印我自己的文本。据我所知,我可以做到这一点
try:
#Calling API here
except:
print('It didn\'t work')
但是如果我得到 400 Bad Request
,我想说“检查你的连接”,对于 500 Internal Server Error
等,我想说“文件无法转换”等等。任何指导将不胜感激。
您可以设置字典,在其中根据特定响应创建自定义消息:
errors = {'400 Bad Request': 'Check your connection'}
try:
#Calling API here
except Exception as e:
print(errors.get('e'))
根据文档,您似乎只使用了响应 header,因此没有充分利用响应 body 的更细粒度的消息。为了做到这一点,你可以遵循这样的事情,甚至可能避免处理异常或构建你自己的字典:
response = # Calling API here
if 'Message' in response:
print(response['Message'])