请求头参数类型错误
今天在做接口自动化项目的时候,某个接口需要在请求头加入特定参数,进行传参
参数类型为“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
转换类型即可