🎓 All Courses | 📚 Python Programming Syllabus

📋 Study this course on TaskLoco
#python-programming#requests#http#apis

requests — HTTP for Humans

The requests library makes HTTP calls simple. Install with pip install requests.


GET request:

import requests

response = requests.get("https://api.example.com/users")
print(response.status_code)  # 200
data = response.json()        # parse JSON body
print(data)

POST request:

payload = {"name": "Alice", "email": "[email protected]"}
response = requests.post(
    "https://api.example.com/users",
    json=payload
)

Headers & auth:

headers = {"Authorization": "Bearer TOKEN"}
response = requests.get(url, headers=headers)

Error handling:

response = requests.get(url)
response.raise_for_status()  # raises exception if 4xx/5xx

if response.status_code == 200:
    data = response.json()
elif response.status_code == 404:
    print("Not found")

Timeouts: Always set them: requests.get(url, timeout=10). Without a timeout your code can hang forever.


YouTube • Top 10
Python Programming: The requests Library
Tap to Watch ›
📸
Google Images • Top 10
Python Programming: The requests Library
Tap to View ›

Reference:

Wikipedia: HTTP

image for linkhttps://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

📚 Python Programming — Full Course Syllabus
📋 Study this course on TaskLoco

TaskLoco™ — The Sticky Note GOAT