HTTP Basics & the requests Library
HTTP is the protocol of the web. The requests library makes HTTP calls simple. Learn GET, POST, headers, status codes, and timeouts.
15 min•By Priygop Team•Updated 2026
HTTP & requests
HTTP & requests
# HTTP Methods:
# GET → Retrieve data
# POST → Send/create data
# PUT → Update data
# DELETE → Remove data
# Status codes:
# 200 → OK
# 201 → Created
# 400 → Bad Request
# 401 → Unauthorized
# 404 → Not Found
# 500 → Server Error
# Using requests library:
# import requests
# response = requests.get("https://api.example.com/users")
# print(response.status_code) # 200
# print(response.json()) # parsed JSON
# Demo: simulating HTTP concepts
class HTTPResponse:
def __init__(self, status_code, data):
self.status_code = status_code
self.data = data
def json(self):
return self.data
def simulate_request(method, url):
print(f" {method} {url}")
responses = {
"/users": HTTPResponse(200, [{"id": 1, "name": "Alice"}]),
"/posts": HTTPResponse(200, [{"id": 1, "title": "Hello"}]),
"/404": HTTPResponse(404, {"error": "Not found"}),
}
path = "/" + url.split("/")[-1]
resp = responses.get(path, HTTPResponse(404, {"error": "Not found"}))
print(f" Status: {resp.status_code}")
print(f" Data: {resp.json()}")
return resp
# Simulated API calls
print("=== HTTP Requests Demo ===")
simulate_request("GET", "https://api.example.com/users")
print()
simulate_request("GET", "https://api.example.com/posts")
print()
simulate_request("GET", "https://api.example.com/404")Tip
Tip
Always check response.status_code before using response.json(). 200 means success, 404 means not found, 500 means server error.
Diagram
Loading diagram…
Every website works on this model
Common Mistake
Warning
Not handling network errors. Always wrap requests in try/except for ConnectionError, Timeout, etc.
Quick Quiz
Practice Task
Note
(1) Make a GET request to a public API. (2) Parse the JSON response. (3) Handle error status codes gracefully.