我创建我的请求类,代码如下:
class RunMethod:
def post_main(self, url, data, header=None):
res = None
if header != None:
res = requests.post(url=url, data=data, headers=header)
else:
res = requests.post(url=url, data=data)
return res.json()
def get_main(self, url, data=None,header=None):
res = None
if header != None:
res = requests.get(url=url,data=data,headers=header,verify=False)
else:
res = requests.get(url=url,data=data,verify=False)
return res.json()
def run_main(self, method,url,data=None,headers=None):
res = None
if method == 'get':
res = self.get_main(url, data, headers)
else:
res = self.post_main(url, data, headers)
我从查尔斯那里捕获了一个api,并在邮递员中对其进行了测试,它返回200。我从邮递员中导出python代码,就像这样:
import requests
url = "https://stargate.ar.elenet.me/minimart.service/intelligent/invoke"
querystring = {"traceId": "1000000294010",
"shelfCode": "lu8ssMgCpgq00FDYdpX76Q..", "tracedAt": "1545641563164"}
payload = ""
headers = {
'X-STARGATE-ACCESS-TOKEN': "d7594351-0663-43a8-ad55-180c8b29db82",
'Cookie': "SID=NTVMAu8FKskyj06ln8J9uhS45fgcRNk1V3jQ; USERID=2228440841",
'Authorization': "ElemeAPI token",
'cache-control': "no-cache"
}
response = requests.request(
"GET", url, data=payload, headers=headers, params=querystring)
print(response.text)
它起作用了,我改变了我的课,将数据像这样放置:
url = "https://stargate.ar.elenet.me/minimart.service/intelligent/invoke?traceId=1000000294010&shelfCode=lu8ssMgCpgq00FDYdpX76Q..&tracedAt=1545641563164"
headers = {
'X-STARGATE-ACCESS-TOKEN': "d7594351-0663-43a8-ad55-180c8b29db82",
'Cookie': "SID=NTVMAu8FKskyj06ln8J9uhS45fgcRNk1V3jQ; USERID=2228440841",
'Authorization': "ElemeAPI token",
'cache-control': "no-cache",
'Content-Type':'application/json'
}
exam = RunMethod()
res = exam.run_main('get', url, headers)
我只是将querystring放入url,但是它返回401。 我不知道哪里出了问题。有人可以帮我吗?非常感谢!
答案 0 :(得分:0)
pool.join()
接受4个参数,run_main
仅提供3个参数(exam.run_main('get', url, headers)
)。
当跳过某些可选参数时,我建议使用命名参数:
method='get', url=url, data=headers, headers=None
答案 1 :(得分:0)
按照以下代码在get_main方法中更新代码。
res = requests.get(url=url,params=data,headers=header,verify=False)
在这里,我们调用实例化的RunMethod类,并调用run_main方法。
exam = RunMethod()
res = exam.run_main('get', url, querystring, headers)