requests二次封装提示得到了意外的关键字参数

来源:9-4 项目实战之主流程中cookie方法操作

慕侠622557

2021-04-19

import requests
from readIni import Read_Init

class Res_Run:
def res_get(self,url,data,header,cookie=None):
’’‘
封装GET方法,传参URL,data, headers
’’'
resGet = requests.get(url=url,data=data,headers=header,cookies=cookie)
return resGet

def res_post(self,url,data,header,cookie=None):
    '''
    封装post方法,传参URL,params, data, headers
    '''
    resPost = requests.post(url,data,header,cookie)
    return resPost

def run_main(self,method,url,data,header=None,cookie=None):
    readInit = Read_Init()
    base_url = readInit.read_file_ini('host')
    if 'http' not in url:
        url = base_url + url


    if method == 'get':
        resRun = self.res_get(url=url,data=data,headers=header,cookies=cookie)
        
    elif method == 'post':
        resRun = self.res_post(url=url,data=data,headers=header,cookies=cookie)
    
    return resRun

reques_main = Res_Run()
if name == ‘main’:
url = '/rest/V1/external/home’
data1 = {
“groups”:50,
“customer_id”:299
}
header = {
“Content-Type”:“application/x-www-form-urlencoded”,“api-version”:“V4”,“jwt-platform”:“android”
}
param = {“groups”:10}
#print(type(param))
#print(type(header))
res = reques_main.run_main(‘get’,url,data=param,headers=header).json()
#res = requests.get(url,headers=header).json()
print(res)

  File "e:/work/study/oneself/API/base/request_method.py", line 47, in <module>
res = reques_main.run_main('get',url,data=param,headers=header).json()     

TypeError: run_main() got an unexpected keyword argument ‘headers’

请教老师,get 封装加了header 和cookie,执行报错,TypeError: run_main() got an unexpected keyword argument ‘headers’,不知道为啥不行

写回答

1回答

Mushishi

2021-04-21

header 你仔细看看 你是传递的headers

0
3
Mushishi
回复
慕侠622557
额。你自己定义的参数名字是header啊。 你结果传递一个headers。他怎么认识呢
2021-04-23
共3条回复

Python接口自动化测试框架实战 从设计到开发

接口自动化测试框架的设计与开发,并教你如何开发桩服务

1720 学习 · 831 问题

查看课程