0% found this document useful (0 votes)
2 views3 pages

Python API Requests Examples

The document provides examples of using the Python Requests library for API automation, demonstrating the GET, POST, PUT, PATCH, and DELETE methods. Each method includes code snippets that show how to make requests, set headers, manage cookies, and perform assertions on the responses. The examples illustrate how to interact with a user API endpoint effectively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Python API Requests Examples

The document provides examples of using the Python Requests library for API automation, demonstrating the GET, POST, PUT, PATCH, and DELETE methods. Each method includes code snippets that show how to make requests, set headers, manage cookies, and perform assertions on the responses. The examples illustrate how to interact with a user API endpoint effectively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

Python Requests API Automation Examples

# GET Method
import requests

url = "[Link]

params = {"page":1, "limit":10}

headers = {
"Authorization":"Bearer token123",
"Accept":"application/json"
}

cookies = {"session_id":"abc123"}

response = [Link](
url=url,
params=params,
headers=headers,
cookies=cookies,
auth=("user","pass"),
timeout=5,
allow_redirects=True,
verify=True
)

# Assertions (What we test)


assert response.status_code == 200
assert "users" in [Link]

print([Link]())

# POST Method
import requests

url = "[Link]

headers = {
"Content-Type":"application/json",
"Authorization":"Bearer token123"
}

json_body = {
"name":"Krishna",
"age":28
}

cookies = {"session_id":"abc123"}

response = [Link](
url=url,
json=json_body,
headers=headers,
cookies=cookies,
files=None,
auth=("user","pass"),
timeout=5
)

# Assertions
assert response.status_code in [200,201]
assert [Link]()["name"] == "Krishna"

print([Link]())

# PUT Method
import requests

url = "[Link]

headers = {"Content-Type":"application/json"}

json_body = {
"name":"Krishna",
"age":30
}

response = [Link](
url=url,
json=json_body,
headers=headers,
cookies={"session_id":"abc123"},
auth=("user","pass"),
timeout=5
)

# Assertions
assert response.status_code == 200
print([Link]())

# PATCH Method
import requests

url = "[Link]

headers = {"Content-Type":"application/json"}

json_body = {
"age":31
}

response = [Link](
url=url,
json=json_body,
headers=headers,
cookies={"session_id":"abc123"},
auth=("user","pass"),
timeout=5
)

# Assertions
assert response.status_code == 200
print([Link]())

# DELETE Method
import requests

url = "[Link]

headers = {
"Authorization":"Bearer token123"
}

response = [Link](
url=url,
headers=headers,
cookies={"session_id":"abc123"},
auth=("user","pass"),
timeout=5
)

# Assertions
assert response.status_code in [200,204]
print("User deleted successfully")

You might also like