http
import httpThe http module provides synchronous HTTP requests backed by libcurl. Both http:// and https:// are supported. Redirects are followed automatically.
All functions return (int, str) — the HTTP status code and the response body as a string.
Functions
| Signature | Description |
|---|---|
http.get(url: str) -> (int, str) | GET request |
http.get(url: str, timeout_ms: int) -> (int, str) | GET with timeout |
http.get(url: str, headers: dict[str, str]) -> (int, str) | GET with custom headers |
http.post(url: str, body: str) -> (int, str) | POST request |
http.post(url: str, body: str, timeout_ms: int) -> (int, str) | POST with timeout |
http.post(url: str, body: str, headers: dict[str, str]) -> (int, str) | POST with custom headers |
Examples
import http
def main() -> None: (status, body) = http.get("https://httpbin.org/get") print(status) print(body)import http
def main() -> None: (status, body) = http.get("https://httpbin.org/get", 5000) print(status)import http
def main() -> None: headers: dict[str, str] = {"Accept": "application/json"} (status, body) = http.get("https://httpbin.org/get", headers) print(status) print(body)import http
def main() -> None: payload: str = "{\"key\": \"value\"}" (status, body) = http.post("https://httpbin.org/post", payload) print(status)Notes
- Transport failures (connection refused, DNS failure, etc.) are runtime errors — they are not returned as status codes.
- HTTP error codes like
404or500are normal return values, not errors. - Timeout values are in milliseconds. Non-positive timeout values are runtime errors.
- The request body and response body are both plain strings. Responses containing NUL bytes are rejected at runtime.
- Response headers are not yet captured; only the body is returned.
libcurlis resolved automatically when your program importshttp. See Installation for details.