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
116 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 | |
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
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
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 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 | |
ok
property
¶
True if status_code is 2xx.
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 sync connection.
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.
Usage::
with Client(headers={"Authorization": "Bearer token"}) as c:
r = c.get("https://api.example.com/data")
Source code in httpclient/httpclient.py
2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 | |
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.
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
2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 | |
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.