How to add header to Python Requests

HOME

Headers = { “Authorization” : ”our_unique_secret_token” }

pip install -U requests

pip install -U pytest

import requests

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


def test_add_header():
    request_body = {
        "name": "Vibha",
        "Job": "CEO"
    }

    header = {"Content-Type":  "application/json; charset=utf-8"}

    response = requests.post(ENDPOINT, request_body, header)
    print(response.json())
    print("Request Header", response.request.headers)
    print("Response Header", response.headers)
    assert response.status_code == 201
    assert response.headers["Content-Type"] == "application/json; charset=utf-8"

assert response.headers["Content-Type"] == "application/json; charset=utf-8"

pytest PostRequest_test.py -s

Leave a comment