Requests in Python

HOME

How to test GET Request Using Python Requests

HOME

pip install -U requests

pip install -U pytest

import requests

ENDPOINT = 'https://reqres.in/api/'
USER = 'users/2'


def test_get_user_successful():
    response = requests.get(ENDPOINT+USER)
    response_body = response.json()
    print(response_body)
    assert response.status_code == 200
    assert response_body["data"]["id"] == 2
    assert response_body["data"]["first_name"] == "Janet"
    assert response_body["data"]["last_name"] == "Weaver"

def test_get_user_unsuccessful():
    response = requests.get(ENDPOINT+USER)
    print(response.json())
    assert response.status_code == 203