请求头参数类型错误:Value for header {userId: 103369} must be of type str or bytes, not <class ‘int‘>

文章讨论了在接口自动化中遇到的因参数类型不符(int转str)导致的请求头错误,提供了将int类型转换为str的修复方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

请求头参数类型错误

今天在做接口自动化项目的时候,某个接口需要在请求头加入特定参数,进行传参
参数类型为“int”类型。
进行测试的时候会报错,错误代码以下

 requests.exceptions.InvalidHeader: Value for header {onlineType: 1} must be of type str or bytes, not <class 'int'>

C:\Program Files\python\lib\site-packages\requests\utils.py:945: InvalidHeader

``	

在这里插入图片描述

原因:因为参数为INT 类型数据,但是请求头请求必须为 str 类型数据

def user_information(token,userId,timestamp,opinion,title,type):
    data = {
        "circle": 0,
         "deviceCode": "RP1A.200720.012",
         "id": 0,
         "opinion": opinion,
         "postUrls": "",
         "title": title,
         "type": type
         }
    head = {
        "token" :token,
        "userId" :userId,
        "content-type" :'application/json',
        "timestamp" :timestamp,
        "version" :'2.2.1',
        "onlineType" :1,
        "devicetype":"android"
    }
    user_info = url.information(data = json.dumps(data),headers = head)
    # print(user_info.json())
    return user_info

解决方案:在请求头中把int数据转为str类型即可

修改后的代码如下:

def user_information(token,userId,timestamp,opinion,title,type):
    data = {
        "circle": 0,
         "deviceCode": "RP1A.200720.012",
         "id": 0,
         "opinion": opinion,
         "postUrls": "",
         "title": title,
         "type": type
         }
    head = {
        "token" :token,
        "userId" :str(userId),
        "content-type" :'application/json',
        "timestamp" :str(timestamp),
        "version" :'2.2.1',
        "onlineType" :str(1),
        "devicetype":"android"
    }
    user_info = url.information(data = json.dumps(data),headers = head)
    # print(user_info.json())
    return user_info

转换类型即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值