Python Forum

Full Version: Pytest API Post call thrown errors
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am performing the API Testing and using the pytest framework. Test is failing all the time with 401 error. Couldn't figure out what was the issue and where am I going wrong. When I manually test it using Postman, it is working.

Here is the code :

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']}
#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)

# make POST request with JSON Input Body
r = requests.post(url, request_json)
print(r.content)

# Verification of the response
def test_result():
    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)
Error is as below :

test_post.py::test_result FAILED [100%]
test__post.py:35 (test_result)
404 != 200

200
404
<Click to see difference>

def test_result():
> assert r.status_code == 200
E assert 404 == 200

test__post.py:37: AssertionError


================================== FAILURES ===================================
_________________________________ test_result _________________________________

def test_result():
> assert r.status_code == 200
E assert 404 == 200

test__post.py:37: AssertionError
============================== 1 failed in 0.42s ==============================
Process finished with exit code 0

Assertion failed

Assertion failed
That's look like you are getting 404 in return.
If you print r.status_code before test function i guess it don't give 200 back.
Example with some working code you can test with.
import requests

request_json = 'yellow'
r = requests.post("http://httpbin.org/post", json={"color": request_json})
print(r.status_code)
print(r.content)

def test_result():
    assert r.status_code == 200

def test_json_result():
    json_data = r.json()  
    assert json_data['json']['color'] == 'yellow'
Output:
E:\div_code\tq λ pytest =========================================================== test session starts =========================================================== platform win32 -- Python 3.7.3, pytest-5.3.1, py-1.8.0, pluggy-0.13.0 rootdir: E:\div_code\tq, inifile: pytest.ini collected 2 items test_200.py ..
Thank you very much. I did it and the suggestion is working. However, the program threw the following error.
I am accessing JSON payload/schema using the .json file constructed with the schema below.

b'{"statusCode":400,"error":"Bad Request","message":"Invalid request payload JSON format"}'
Looks like my JSON schema formatting is incorrect.

Here is my JSON Schema. Is there a specific way to build it ?

{
  "_id": "5deac2277",
  "marid": 1105960446
}
Try using the json parameter json=request_json.
import requests

request_json = {
  "_id": "5deac2277",
  "marid": 1105960446
}

r = requests.post("http://httpbin.org/post", json=request_json)
Output:
>>> r.status_code 200 >>> j = r.json() >>> j.get('json')['_id'] '5deac2277' >>> j.get('json')['marid'] 1105960446
Thank you so very much.
Right way is the request to send as :
r = requests.post(url, json=request_json, headers=headers)
not as : data=request_json

I did the "data=request_json" and hence it is failing.

I really appreciate your help. This has solved many in our list to test.
I have no words to thank you, but
Merry Christmas
.