You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 03:24:01