api.md
This file
Path
docs/api.md
162 lines · 5.8 KiB · Markdown
Last changed by
Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
History of this fileTake it
http3 (#86)Tom Christiebefore adoptionc9747aa
lines 1–2why, one step back1# Developer Interface2Force HTTP/1.1 on short-lived connections (#284)Seth Michael Larsonbefore adoptione7aa6d6
line 3why, one step back3## Helper Functionshttp3 (#86)Tom Christiebefore adoptionc9747aa
line 4why, one step back4Force HTTP/1.1 on short-lived connections (#284)Seth Michael Larsonbefore adoptione7aa6d6
lines 5–10why, one step back5!!! note6 Only use these functions if you're testing HTTPX in a console7 or making a small number of requests. Using a `Client` will8 enable HTTP/2 and connection pooling for more efficient and9 long-lived connections.10http3 (#86)Tom Christiebefore adoptionc9747aa
lines 11–18why, one step back11* `get(url, [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`12* `options(url, [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`13* `head(url, [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`14* `post(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`15* `put(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`16* `patch(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`17* `delete(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`18* `request(method, url, [data], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac
line 19why, one step back19* `build_request(method, url, [data], [files], [json], [params], [headers], [cookies])`http3 (#86)Tom Christiebefore adoptionc9747aa
lines 20–22why, one step back2021## `Client`22Force HTTP/1.1 on short-lived connections (#284)Seth Michael Larsonbefore adoptione7aa6d6
line 23why, one step back23*An HTTP client, with connection pooling, HTTP/2, redirects, cookie persistence, etc.*http3 (#86)Tom Christiebefore adoptionc9747aa
lines 24–25why, one step back2425```pythonRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 26why, one step back26>>> client = httpx.Client()http3 (#86)Tom Christiebefore adoptionc9747aa
lines 27–29why, one step back27>>> response = client.get('https://example.org')28```29Allow setting headers at the Client level (#159)Stephen Brown IIbefore adoption72e6f47
line 30why, one step back30* `def __init__([auth], [headers], [cookies], [verify], [cert], [timeout], [pool_limits], [max_redirects], [app], [dispatch])`Automatically convert Client.cookies and Client.headers on set (#274)toppkbefore adoption9e420a5
lines 31–32why, one step back31* `.headers` - **Headers**32* `.cookies` - **Cookies**http3 (#86)Tom Christiebefore adoptionc9747aa
lines 33–40why, one step back33* `def .get(url, [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`34* `def .options(url, [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`35* `def .head(url, [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`36* `def .post(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`37* `def .put(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`38* `def .patch(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`39* `def .delete(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`40* `def .request(method, url, [data], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [verify], [cert], [timeout])`Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac
line 41why, one step back41* `def .build_request(method, url, [data], [files], [json], [params], [headers], [cookies])`http3 (#86)Tom Christiebefore adoptionc9747aa
lines 42–49why, one step back42* `def .send(request, [stream], [allow_redirects], [verify], [cert], [timeout])`43* `def .close()`4445## `Response`4647*An HTTP response.*4849* `def __init__(...)`Int status codes (#92)Tom Christiebefore adoption56a7943
line 50why, one step back50* `.status_code` - **int**http3 (#86)Tom Christiebefore adoptionc9747aa
line 51why, one step back51* `.reason_phrase` - **str**Response.protocol -> Response.http_version (#250)Tom Christiebefore adoptionc1ec1c3
line 52why, one step back52* `.http_version` - `"HTTP/2"` or `"HTTP/1.1"`http3 (#86)Tom Christiebefore adoptionc9747aa
lines 53–61why, one step back53* `.url` - **URL**54* `.headers` - **Headers**55* `.content` - **bytes**56* `.text` - **str**57* `.encoding` - **str**58* `.is_redirect` - **bool**59* `.request` - **Request**60* `.cookies` - **Cookies**61* `.history` - **List[Response]**Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
lines 62–66why, one step back62* `.elapsed` - **[timedelta](https://docs.python.org/3/library/datetime.html)**63 * The amount of time elapsed between sending the first byte and parsing the headers (not including time spent reading64 the response). Use65 [total_seconds()](https://docs.python.org/3/library/datetime.html#datetime.timedelta.total_seconds) to correctly get66 the total elapsed seconds.http3 (#86)Tom Christiebefore adoptionc9747aa
lines 67–80why, one step back67* `def .raise_for_status()` - **None**68* `def .json()` - **Any**69* `def .read()` - **bytes**70* `def .stream()` - **bytes iterator**71* `def .raw()` - **bytes iterator**72* `def .close()` - **None**73* `def .next()` - **Response**7475## `Request`7677*An HTTP request. Can be constructed explicitly for more control over exactly78what gets sent over the wire.*7980```pythonRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 81why, one step back81>>> request = httpx.Request("GET", "https://example.org", headers={'host': 'example.org'})http3 (#86)Tom Christiebefore adoptionc9747aa
lines 82–127why, one step back82>>> response = client.send(request)83```8485* `def __init__(method, url, [params], [data], [json], [headers], [cookies])`86* `.method` - **str**87* `.url` - **URL**88* `.content` - **byte** or **byte async iterator**89* `.headers` - **Headers**90* `.cookies` - **Cookies**9192## `URL`9394*A normalized, IDNA supporting URL.*9596```python97>>> url = URL("https://example.org/")98>>> url.host99'example.org'100```101102* `def __init__(url, allow_relative=False, params=None)`103* `.scheme` - **str**104* `.authority` - **str**105* `.host` - **str**106* `.port` - **int**107* `.path` - **str**108* `.query` - **str**109* `.full_path` - **str**110* `.fragment` - **str**111* `.is_ssl` - **bool**112* `.origin` - **Origin**113* `.is_absolute_url` - **bool**114* `.is_relative_url` - **bool**115* `def .copy_with([scheme], [authority], [path], [query], [fragment])` - **URL**116* `def .resolve_with(url)` - **URL**117118## `Origin`119120*A normalized, IDNA supporting set of scheme/host/port info.*121122```python123>>> Origin('https://example.org') == Origin('HTTPS://EXAMPLE.ORG:443')124True125```126127* `def __init__(url)`Make Origin use scheme, not is_ssl (#168)Stephen Brown IIbefore adoption44beaa1
line 128why, one step back128* `.scheme` - **str**http3 (#86)Tom Christiebefore adoptionc9747aa
lines 129–162why, one step back129* `.is_ssl` - **bool**130* `.host` - **str**131* `.port` - **int**132133## `Headers`134135*A case-insensitive multi-dict.*136137```python138>>> headers = Headers({'Content-Type': 'application/json'})139>>> headers['content-type']140'application/json'141```142143* `def __init__(self, headers)`144145## `Cookies`146147*A dict-like cookie store.*148149```python150>>> cookies = Cookies()151>>> cookies.set("name", "value", domain="example.org")152```153154* `def __init__(cookies: [dict, Cookies, CookieJar])`155* `.jar` - **CookieJar**156* `def extract_cookies(response)`157* `def set_cookie_header(request)`158* `def set(name, value, [domain], [path])`159* `def get(name, [domain], [path])`160* `def delete(name, [domain], [path])`161* `def clear([domain], [path])`162* *Standard mutable mapping interface*