Skip to content
Myr

advanced.md

This file

Path

docs/advanced.md

162 lines · 4.5 KiB · Markdown

Last changed by

Fix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5

History of this file

Take it

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

lines 1–9why, one step back
1# Advanced Usage
23## Client Instances45Using a Client instance to make requests will give you HTTP connection pooling,6will provide cookie persistence, and allows you to apply configuration across7all outgoing requests.89```python

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 10why, one step back
10>>> client = httpx.Client()

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

lines 11–17why, one step back
11>>> r = client.get('https://example.org/')
12>>> r13<Response [200 OK]>14```1516## Calling into Python Web Apps17

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 18why, one step back
18You can configure an `httpx` client to call directly into a Python web

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

lines 19–22why, one step back
19application, using either the WSGI or ASGI protocol.
2021This is particularly useful for two main use-cases:22

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 23why, one step back
23* Using `httpx` as a client, inside test cases.

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

lines 24–29why, one step back
24* Mocking out external services, during tests or in dev/staging environments.
2526Here's an example of integrating against a Flask application:2728```python29from flask import Flask

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 30why, one step back
30import httpx

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

lines 31–38why, one step back
31
3233app = Flask(__name__)3435@app.route("/")36def hello():37 return "Hello World!"38

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 39why, one step back
39client = httpx.Client(app=app)

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

lines 40–43why, one step back
40r = client.get('http://example/')
41assert r.status_code == 20042assert r.text == "Hello World!"43```

Documented netrc support (#225)Can Sarıgölbefore adoption21939fd

line 44why, one step back
44

Fix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5

line 45why, one step back
45For some more complex cases, you might need to customize the WSGI or ASGI

Drop `raise_app_exceptions` Client argument. (#238)Tom Christiebefore adoptionef9fc0f

lines 46–55why, one step back
46dispatch. This allows you to:
4748* Inspect 500 error responses, rather than raise exceptions, by setting `raise_app_exceptions=False`.49* Mount the WSGI or ASGI application at a subpath, by setting `script_name` (WSGI) or `root_path` (ASGI).50* Use a given the client address for requests, by setting `remote_addr` (WSGI) or `client` (ASGI).5152For example:5354```python55# Instantiate a client that makes WSGI requests with a client IP of "1.2.3.4".

Expose ASGIDispatch & WSGIDispatch in the 'dispatch' namespace. (#407)Ahmed Maherbefore adoption5ced56b

line 56why, one step back
56dispatch = httpx.dispatch.WSGIDispatch(app=app, remote_addr="1.2.3.4")

Drop `raise_app_exceptions` Client argument. (#238)Tom Christiebefore adoptionef9fc0f

lines 57–59why, one step back
57client = httpx.Client(dispatch=dispatch)
58```59

Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac

lines 60–72why, one step back
60## Build Request
6162You can use `Client.build_request()` to build a request and63make modifications before sending the request.6465```python66>>> client = httpx.Client()67>>> req = client.build_request("OPTIONS", "https://example.com")68>>> req.url.full_path = "*" # Build an 'OPTIONS *' request for CORS69>>> client.send(r)70<Response [200 OK]>71```72

Fix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5

line 73why, one step back
73## Specify the version of the HTTP protocol

Add example of specifying HTTP versions (#337)Paul (xobb) Chubatyybefore adoption0c4460e

line 74why, one step back
74

Fix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5

line 75why, one step back
75One can set the version of the HTTP protocol for the client in case you want to make the requests using a specific version.

Add example of specifying HTTP versions (#337)Paul (xobb) Chubatyybefore adoption0c4460e

lines 76–86why, one step back
76
77For example:7879```python80h11_client = httpx.Client(http_versions=["HTTP/1.1"])81h11_response = h11_client.get("https://myserver.com")8283h2_client = httpx.Client(http_versions=["HTTP/2"])84h2_response = h2_client.get("https://myserver.com")85```86

Documented netrc support (#225)Can Sarıgölbefore adoption21939fd

lines 87–113why, one step back
87## .netrc Support
8889HTTPX supports .netrc file. In `trust_env=True` cases, if auth parameter is90not defined, HTTPX tries to add auth into request's header from .netrc file.9192As default `trust_env` is true. To set false:93```python94>>> httpx.get('https://example.org/', trust_env=False)95```9697If `NETRC` environment is empty, HTTPX tries to use default files.98(`~/.netrc`, `~/_netrc`)99100To change `NETRC` environment:101```python102>>> import os103>>> os.environ["NETRC"] = "my_default_folder/.my_netrc"104```105106.netrc file content example:107```108machine netrcexample.org109login example-username110password example-password111112...113```

Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085

lines 114–159why, one step back
114
115## HTTP Proxying116117HTTPX supports setting up proxies the same way that Requests does via the `proxies` parameter.118For example to forward all HTTP traffic to `http://127.0.0.1:3080` and all HTTPS traffic119to `http://127.0.0.1:3081` your `proxies` config would look like this:120121```python122>>> client = httpx.Client(proxies={123 "http": "http://127.0.0.1:3080",124 "https": "http://127.0.0.1:3081"125})126```127128Proxies can be configured for a specific scheme and host, all schemes of a host,129all hosts for a scheme, or for all requests. When determining which proxy configuration130to use for a given request this same order is used.131132```python133>>> client = httpx.Client(proxies={134 "http://example.com": "...", # Host+Scheme135 "all://example.com": "...", # Host136 "http": "...", # Scheme137 "all": "...", # All138})139>>> client = httpx.Client(proxies="...") # Shortcut for 'all'140```141142!!! warning143 To make sure that proxies cannot read your traffic,144 and even if the proxy_url uses HTTPS, it is recommended to145 use HTTPS and tunnel requests if possible.146147By default `HTTPProxy` will operate as a forwarding proxy for `http://...` requests148and will establish a `CONNECT` TCP tunnel for `https://` requests. This doesn't change149regardless of the `proxy_url` being `http` or `https`.150151Proxies can be configured to have different behavior such as forwarding or tunneling all requests:152153```python154proxy = httpx.HTTPProxy(155 proxy_url="https://127.0.0.1",156 proxy_mode=httpx.HTTPProxyMode.TUNNEL_ONLY157)158client = httpx.Client(proxies=proxy)159

Fix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5

line 160why, one step back
160# This request will be tunneled instead of forwarded.

Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085

lines 161–162why, one step back
161client.get("http://example.com")
162```