{
    "slug": "py_requests_httpx",
    "term": "HTTP in Python — requests & httpx",
    "category": "python",
    "difficulty": "beginner",
    "short": "requests is the standard sync HTTP library; httpx adds async support, HTTP/2, and a similar API — both far more ergonomic than urllib.",
    "long": "requests: synchronous HTTP with a clean API — Session for connection pooling, automatic JSON encoding/decoding, timeout parameter (always set it), retry with HTTPAdapter. httpx: requests-compatible API plus async (async with httpx.AsyncClient()), HTTP/2 support, better streaming. Key practices: always set timeout, use sessions for multiple requests to the same host (connection reuse), check response.raise_for_status() rather than manual status code checking, and never disable SSL verification (verify=False is insecure).",
    "aliases": [
        "requests library",
        "httpx",
        "Python HTTP",
        "urllib"
    ],
    "tags": [
        "python",
        "http",
        "async"
    ],
    "misconception": "urllib is equivalent to requests — urllib requires manual encoding, header management, and error handling; requests provides a dramatically simpler API with the same power.",
    "why_it_matters": "HTTP without timeout is a reliability disaster — a single slow upstream server hangs the entire process indefinitely; always setting timeout is the difference between a resilient and a fragile service.",
    "common_mistakes": [
        "No timeout parameter — process hangs indefinitely on slow servers.",
        "Not using Session for multiple requests — creates a new TCP connection per request.",
        "verify=False to bypass SSL — disables certificate verification, enabling MITM.",
        "Not calling raise_for_status() — 400/500 responses silently succeed without it."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "py_async",
        "py_context_managers",
        "encryption_in_transit"
    ],
    "prerequisites": [
        "http_request_response_cycle",
        "py_async",
        "py_error_handling"
    ],
    "refs": [
        "https://docs.python-requests.org/",
        "https://www.python-httpx.org/"
    ],
    "bad_code": "import requests\n# No timeout — hangs forever if server is slow:\nresponse = requests.get('https://api.example.com/data')\n# No error check — 500 treated as success:\ndata = response.json()\n\n# New connection every request:\nfor url in urls:\n    requests.get(url)  # New TCP connection each time",
    "good_code": "import requests\nfrom requests.adapters import HTTPAdapter\nfrom urllib3.util.retry import Retry\n\n# Session with retry and timeout:\nsession = requests.Session()\nretry = Retry(total=3, backoff_factor=0.3, status_forcelist=[500, 502, 503])\nsession.mount('https://', HTTPAdapter(max_retries=retry))\n\nresponse = session.get('https://api.example.com/data', timeout=(3.05, 27))\nresponse.raise_for_status()  # Raises for 4xx/5xx\ndata = response.json()\n\n# Async with httpx:\nasync with httpx.AsyncClient(timeout=30) as client:\n    response = await client.get(url)\n    response.raise_for_status()",
    "quick_fix": "Use httpx instead of requests for new Python code — it has the same API but supports async, HTTP/2, and proper timeout defaults; always set explicit timeouts on all HTTP calls",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/py_requests_httpx",
        "html_url": "https://codeclaritylab.com/glossary/py_requests_httpx",
        "json_url": "https://codeclaritylab.com/glossary/py_requests_httpx.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[HTTP in Python — requests & httpx](https://codeclaritylab.com/glossary/py_requests_httpx) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/py_requests_httpx"
            }
        }
    }
}