HTTP Client API Reference¶
Auto-generated API documentation for the HTTP client module.
httpclient
¶
Zero-dependency sync + async HTTP REST client.
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
Sync (http.client) and async (asyncio streams) HTTP/1.1 client for REST API consumption. Thread-safe by design.
Sync usage::
response = get("https://httpbin.org/get")
response.json()
Async usage::
response = await async_get("https://httpbin.org/get")
response.json()
Session usage::
with Client() as client:
r = client.get("https://httpbin.org/get")
async with AsyncClient() as client:
r = await client.get("https://httpbin.org/get")
CaseInsensitiveDict
¶
Bases: dict
Case-insensitive key lookup dict subclass that preserves original casing.
Provides case-insensitive HTTP header storage: d["Content-Type"]
and d["content-type"] resolve to the same slot, but iteration and
wire serialisation yield the original casing the caller supplied.
Internally the underlying dict stores {lowercase_key: value}
for O(1) lookups, while a parallel _keys mapping records
{lowercase_key: original_key} for casing-preserving iteration.
This is the type used for Response.headers,
StreamingResponse.headers, and the internal req_headers dict
that flows through _prepare_request. HTTP header names are
case-insensitive per :rfc:7230 §3.2, but the wire format and
echo tests expect the casing the caller supplied.
It is a dict subclass, so it is accepted everywhere a dict
is expected. Equality is case-insensitive on keys:
CaseInsensitiveDict({"X-Foo": "bar"}) == {"x-foo": "bar"}.
Source code in httpclient/httpclient.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
items()
¶
Yield (original_key, value) pairs; preserves casing on the wire.
HttpClientError
¶
HTTPError
¶
Bases: HttpClientError
Raised on non-2xx status when raise_for_status() is called.
Source code in httpclient/httpclient.py
TooManyRedirects
¶
HttpConnectionError
¶
Bases: HttpClientError
Raised on connection failures.
Attributes:
| Name | Type | Description |
|---|---|---|
host |
Remote hostname that the connection targeted. |
|
port |
Remote port number. |
|
message |
Human-readable error description. |
Source code in httpclient/httpclient.py
HttpTimeoutError
¶
Bases: HttpClientError
Raised on request timeout.
Attributes:
| Name | Type | Description |
|---|---|---|
url |
The URL that timed out. |
|
timeout |
The timeout value in seconds that was exceeded. |
|
message |
Human-readable error description. |
Source code in httpclient/httpclient.py
Socks5Error
¶
Bases: HttpConnectionError
Raised on SOCKS5 proxy handshake failures.
Source code in httpclient/httpclient.py
Response
¶
HTTP response object.
Attributes:
| Name | Type | Description |
|---|---|---|
status_code |
HTTP status code. |
|
headers |
Response headers as dict (last value wins for duplicates). |
|
content |
Raw response body as bytes. |
|
url |
Final URL after redirects. |
Source code in httpclient/httpclient.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
Auth
¶
Base class for HTTP authentication.
Source code in httpclient/httpclient.py
auth_headers(method, url)
¶
Return authorization headers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
HTTP method. |
required |
url
|
str
|
Request URL. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dict of headers to add to the request. |
Source code in httpclient/httpclient.py
BasicAuth
¶
Bases: Auth
HTTP Basic authentication.
Source code in httpclient/httpclient.py
auth_headers(method, url)
¶
Return Basic Authorization header.
DigestAuth
¶
Bases: Auth
HTTP Digest authentication.
Source code in httpclient/httpclient.py
auth_headers(method, url)
¶
auth_headers_from_challenge(method, path, challenge)
¶
Compute Digest auth headers from a WWW-Authenticate challenge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
HTTP method. |
required |
path
|
str
|
Request path (URI). |
required |
challenge
|
str
|
The WWW-Authenticate header value. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dict with the Authorization header. |
Source code in httpclient/httpclient.py
StreamingResponse
¶
HTTP streaming response -- holds the connection open.
Use as a context manager to ensure cleanup::
with get(url, stream=True) as r:
for chunk in r.iter_bytes():
process(chunk)
async with await async_get(url, stream=True) as r:
async for line in r.aiter_lines():
handle(line)
Source code in httpclient/httpclient.py
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 | |
ok
property
¶
True if status_code is 2xx.
cookies
property
¶
Parse Set-Cookie headers into a {name: value} dict (cached).
raise_for_status()
¶
iter_bytes(chunk_size=4096)
¶
Yield response body in chunks.
Source code in httpclient/httpclient.py
iter_lines()
¶
Yield response body line by line (decoded).
Source code in httpclient/httpclient.py
read()
¶
aiter_bytes(chunk_size=4096)
async
¶
Async yield response body in chunks.
Source code in httpclient/httpclient.py
aiter_lines()
async
¶
Async yield response body line by line (decoded).
Source code in httpclient/httpclient.py
aread()
async
¶
close()
¶
Close the underlying connection (sync or async best-effort).
Source code in httpclient/httpclient.py
aclose()
async
¶
Close the underlying async connection.
Source code in httpclient/httpclient.py
Client
¶
Synchronous HTTP client session with connection pooling.
Thread-safe: the underlying connection pool uses its own
threading.Lock to protect shared state.
The cookie jar is a flat dict[str, str] — cookies are not
scoped by domain or path. This is suitable for single-origin use
(e.g. an API client talking to one service). For multi-origin
scenarios, manage cookies per-domain yourself.
Usage::
with Client(headers={"Authorization": "Bearer token"}) as c:
r = c.get("https://api.example.com/data")
Source code in httpclient/httpclient.py
2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 | |
request(method, url, **kwargs)
¶
Send an HTTP request.
Source code in httpclient/httpclient.py
close()
¶
AsyncClient
¶
Asynchronous HTTP client session with connection pooling.
Safe for concurrent use from multiple asyncio tasks. The underlying
connection pool uses its own asyncio.Lock to protect shared state.
The cookie jar is a flat dict[str, str] — cookies are not
scoped by domain or path. This is suitable for single-origin use
(e.g. an API client talking to one service). For multi-origin
scenarios, manage cookies per-domain yourself.
Usage::
async with AsyncClient(headers={"Authorization": "Bearer token"}) as c:
r = await c.get("https://api.example.com/data")
Source code in httpclient/httpclient.py
2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 | |
request(method, url, **kwargs)
async
¶
Send an async HTTP request.
Source code in httpclient/httpclient.py
aclose()
async
¶
close()
¶
Emit a warning and do nothing — use await aclose() instead.
AsyncClient manages async resources; calling synchronous close()
cannot safely await the pool teardown coroutine. This method exists
solely for interface parity with :class:Client so that type-annotated
code that calls client.close() does not raise AttributeError.
Always prefer :meth:aclose inside async code.