Python API POST调用在使用pytest时引发401错误

Python API POST调用在使用pytest时引发401错误,python,api,pytest,Python,Api,Pytest,我正在执行API测试并使用pytest框架。测试一直失败,出现401错误。不知道是什么问题 代码如下: 我解决了这个问题,只需将json=your_有效负载 import requests import json,jsonpath import urllib3 import constants urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWar

我正在执行API测试并使用pytest框架。测试一直失败,出现401错误。不知道是什么问题

代码如下:
我解决了这个问题,只需将json=your_有效负载

        import requests
        import json,jsonpath
        import urllib3
        import constants
        urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

        # variables
        dumpFile = "somepath"
        url = "someUrl"
        headers = {'Authorization' : constants.consts['siteToken'],
               'accept':'application/json',
               'content-type':'application/json'}
        #siteToken = 'Bearer jwt token'

        # read json input file
        input_file = open("json file path", 'r')
        json_input = input_file.read()
        request_json = json.loads(json_input)

    def test_json_result():
# make POST request with JSON Input Body
        r = requests.post(url, json=request_json, headers=headers)

        # Verification of the response
        assert r.status_code == 200
        # fetch header from response
        print(r.headers.get("Date"))

        # parse response to JSON Format
        response_json = json.loads(r.text)

        # validate response using Json Path
        name = jsonpath.jsonpath(response_json, 'name')
        print(name)

可能是因为请求中不包括带有身份验证令牌的headers变量。谢谢,我这样做是通过添加:r=requests.postrl,request\u json,headers=headers。现在,程序抛出400错误,我间歇性地得到错误:b'{statusCode:400,error:Bad Request,message:Invalid Request payload JSON format}'。有什么建议吗?是否有一种特定的方法来构建pytest的json?没有足够的细节可以确定,请使用调试器或printr确保传递给请求的url是正确的。我昨晚已经解决了这个问题。这是因为我的要求。它应该是json=request_json,而不仅仅是request_json。
        import requests
        import json,jsonpath
        import urllib3
        import constants
        urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

        # variables
        dumpFile = "somepath"
        url = "someUrl"
        headers = {'Authorization' : constants.consts['siteToken'],
               'accept':'application/json',
               'content-type':'application/json'}
        #siteToken = 'Bearer jwt token'

        # read json input file
        input_file = open("json file path", 'r')
        json_input = input_file.read()
        request_json = json.loads(json_input)

    def test_json_result():
# make POST request with JSON Input Body
        r = requests.post(url, json=request_json, headers=headers)

        # Verification of the response
        assert r.status_code == 200
        # fetch header from response
        print(r.headers.get("Date"))

        # parse response to JSON Format
        response_json = json.loads(r.text)

        # validate response using Json Path
        name = jsonpath.jsonpath(response_json, 'name')
        print(name)