我的 python 请求返回 401 而邮递员返回 200

我做了我的请求类,代码如下:


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)

我只是将查询字符串放入 url,但它返回 401。我不知道它错在哪里。有人可以帮我吗?多谢!


泛舟湖上清波郎朗
浏览 299回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python