Python使用requests库在请求头传递整数字段报错如何解决?
报错原因
你遇到的报错是requests库的请求头类型校验规则触发的:
requests.exceptions.InvalidHeader: Value for header {orgId: 721067787} must be of type str or bytes, not <class 'int'>
requests 要求所有请求头的取值只能是字符串(str)或字节(bytes)类型,你的代码中organizationId变量是整数类型,直接放入请求头字典就会触发校验失败。
解决方案
将orgId对应的取值强制转为字符串即可,以下是修正后的代码示例:
authenticationHeaders = {'Authorization': tmpAuthToken, 'orgId': str(organizationId)} response = requests.get("https://desk.zoho.com/api/v1/tickets", headers = authenticationHeaders) return response.json()
如果需要多处使用该ID,也可以提前对变量做类型转换:
organizationId = str(organizationId) authenticationHeaders = {'Authorization': tmpAuthToken, 'orgId': organizationId} response = requests.get("https://desk.zoho.com/api/v1/tickets", headers = authenticationHeaders) return response.json()
内容的提问来源于stack exchange,提问作者Keshav Gambhir
相关产品推荐
相关产品推荐

