🌐 Python Tutorial — HTTP Requests (requests module)

Introduction 🌟

The requests module is the most popular and user-friendly HTTP client for Python. It allows you to send HTTP requests easily — ideal for APIs, web scraping, automation, and testing.

Note

💡 Not built-in → install separately
💡 Simple API → handles cookies, headers, sessions
💡 Supports GET, POST, PUT, DELETE, PATCH, FILE UPLOADS, AUTH

1. Installing requests 📦

install.sh

pip install requests

2. Sending a GET Request 🌍

get_request.py

import requests

response = requests.get("https://api.github.com")

print(response.status_code)
print(response.text)

✔ GET is used to retrieve data from a server

3. Sending a GET Request with Query Params 🔍

params.py

payload = {"search": "python", "page": 2}

response = requests.get("https://example.com", params=payload)

print(response.url)

✔ Automatically builds the URL: ?search=python&page=2

4. Sending a POST Request ➕

post_request.py

data = {"name": "Sathish", "age": 25}

response = requests.post("https://httpbin.org/post", data=data)

print(response.json())

✔ POST is used to send data to the server

5. Sending JSON Data 🧩

json_request.py

response = requests.post(
    "https://httpbin.org/post",
    json={"name": "Kumar", "age": 30}
)

print(response.json())

✔ Automatically sets Content-Type: application/json

6. Custom Headers 📬

custom_headers.py

headers = {"User-Agent": "MyApp/1.0"}

response = requests.get("https://api.github.com", headers=headers)

print(response.status_code)

7. Sending Cookies 🍪

cookies.py

cookies = {"session_id": "abc123"}

response = requests.get("https://example.com", cookies=cookies)

8. Handling Response JSON 📘

json_response.py

res = requests.get("https://api.github.com/users/octocat")

data = res.json()
print(data["login"], data["id"])

9. Checking Response Status Codes ✔️

status_codes.py

res = requests.get("https://example.com")

if res.ok:
    print("Success")
else:
    print("Failed:", res.status_code)

10. Handling Timeouts ⏱️

timeout.py

res = requests.get("https://example.com", timeout=3)

Note

⚠️ Always set a timeout for production systems.

11. Error Handling (Exceptions) ⚠️

exception_handling.py

try:
    response = requests.get("https://invalid.url")
except requests.exceptions.RequestException as e:
    print("Error:", e)

✔ Catches all HTTP-related errors

12. File Uploads 📤

file_upload.py

files = {"file": open("sample.txt", "rb")}

res = requests.post("https://httpbin.org/post", files=files)
print(res.json())

13. Sessions — Persist Cookies & Headers 🔄

Sessions maintain state across multiple requests.

session.py

s = requests.Session()

s.headers.update({"User-Agent": "MySession"})

res1 = s.get("https://httpbin.org/cookies/set?cookie=value")
res2 = s.get("https://httpbin.org/cookies")

print(res2.json())

✔ Useful for login-based apps

14. Authentication 🔐

Basic Auth

basic_auth.py

from requests.auth import HTTPBasicAuth

res = requests.get(
    "https://httpbin.org/basic-auth/user/pass",
    auth=HTTPBasicAuth("user", "pass")
)

print(res.json())

Token Auth

token_auth.py

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

res = requests.get("https://api.example.com/user", headers=headers)

15. Redirects 🔀

redirects.py

res = requests.get("https://httpbin.org/redirect/1")

print(res.history)
print(res.url)

✔ Handles redirects automatically unless disabled

16. Streaming Responses (Large Downloads) 📥

stream_download.py

res = requests.get("https://example.com/largefile", stream=True)

with open("file.bin", "wb") as f:
    for chunk in res.iter_content(chunk_size=1024):
        if chunk:
            f.write(chunk)

17. Proxy Support 🌐

proxy.py

proxies = {
    "http": "http://127.0.0.1:8080",
    "https": "http://127.0.0.1:8080",
}

res = requests.get("https://example.com", proxies=proxies)

18. Real-World Example — REST API Request 🧩

real_api.py

BASE = "https://jsonplaceholder.typicode.com"

posts = requests.get(f"{BASE}/posts").json()

for p in posts[:3]:
    print(p["id"], p["title"])

HTTP Methods Cheat Sheet 📘

MethodPurpose
GETFetch data
POSTCreate new data
PUTFull update
PATCHPartial update
DELETERemove data

Best Practices 💡

  • ✔ Always set timeouts
  • ✔ Use sessions for repeated requests
  • ✔ Use JSON instead of form-data when possible
  • ✔ Handle exceptions using try/except
  • ✔ Avoid scraping without permission

Conclusion 🎉

>>“The requests module makes HTTP communication simple, elegant, and incredibly powerful for modern Python applications.” ✨

You now understand HTTP Requests in Python! Want the next topic? Try API Authentication, REST API Development, Web Scraping with BeautifulSoup, or Async HTTP with aiohttp. Just tell me! 😊