Python Forum
Pytest API Post call thrown errors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pytest API Post call thrown errors
#1
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
Reply
#2
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 ..
Reply
#3
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
}
Reply
#4
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
Reply
#5
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
.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pytest Installed, but VS Code Won’t Access Pytest AstralWeeks 9 3,319 Sep-13-2023, 03:00 PM
Last Post: AstralWeeks
  Pytest mocks anthonyrmoss78 0 453 May-30-2023, 08:28 PM
Last Post: anthonyrmoss78
  which exception should be thrown for a sequence of the wrong length? Skaperen 1 851 Jan-06-2023, 04:13 AM
Last Post: deanhystad
  Pytest and rootdirectory Master_Sergius 4 4,691 Jun-01-2020, 05:05 PM
Last Post: Master_Sergius
  Invalid argument error thrown. pyseeker 4 8,621 Sep-10-2019, 07:03 PM
Last Post: pyseeker
  Pytest with requests a21250450 3 2,775 Mar-21-2019, 03:24 PM
Last Post: buran
  pytest fixture in conftest.py thrown error while in the test file runs OzzieOzzum 1 3,979 Jul-31-2018, 12:12 PM
Last Post: OzzieOzzum
  [split] Teacher (thrown in at the deep end - help) Mr90 2 3,019 May-23-2018, 02:04 PM
Last Post: DeaD_EyE
  Teacher (thrown in at the deep end - help) Mr90 5 3,894 May-22-2018, 01:08 PM
Last Post: DeaD_EyE
  pytest and caplog lazyliv 0 3,080 May-16-2018, 02:36 PM
Last Post: lazyliv

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020