advanced.md
This file
Path
docs/advanced.md
164 lines · 4.6 KiB · Markdown
Last changed by
Fix some grammar in Advanced docs (#461)thebigmunchbefore adoption3917866
History of this fileTake it
Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 1–8why, one step back1# Advanced Usage23## 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.8Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7
lines 9–10why, one step back9A Client instance is equivalent to a Session instance in `requests`.10Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
line 11why, one step back11```pythonRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 12why, one step back12>>> client = httpx.Client()Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 13–19why, one step back13>>> r = client.get('https://example.org/')14>>> r15<Response [200 OK]>16```1718## Calling into Python Web Apps19Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 20why, one step back20You can configure an `httpx` client to call directly into a Python webFix some grammar in Advanced docs (#461)thebigmunchbefore adoption3917866
line 21why, one step back21application using either the WSGI or ASGI protocol.Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 22–24why, one step back2223This is particularly useful for two main use-cases:24Fix some grammar in Advanced docs (#461)thebigmunchbefore adoption3917866
lines 25–26why, one step back25* Using `httpx` as a client inside test cases.26* Mocking out external services during tests or in dev/staging environments.Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 27–31why, one step back2728Here's an example of integrating against a Flask application:2930```python31from flask import FlaskRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 32why, one step back32import httpxSupport WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 33–40why, one step back333435app = Flask(__name__)3637@app.route("/")38def hello():39 return "Hello World!"40Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 41why, one step back41client = httpx.Client(app=app)Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 42–45why, one step back42r = client.get('http://example/')43assert r.status_code == 20044assert r.text == "Hello World!"45```Documented netrc support (#225)Can Sarıgölbefore adoption21939fd
line 46why, one step back46Fix some grammar in Advanced docs (#461)thebigmunchbefore adoption3917866
line 47why, one step back47For some more complex cases you might need to customize the WSGI or ASGIDrop `raise_app_exceptions` Client argument. (#238)Tom Christiebefore adoptionef9fc0f
lines 48–49why, one step back48dispatch. This allows you to:49Fix some grammar in Advanced docs (#461)thebigmunchbefore adoption3917866
lines 50–52why, one step back50* Inspect 500 error responses rather than raise exceptions by setting `raise_app_exceptions=False`.51* Mount the WSGI or ASGI application at a subpath by setting `script_name` (WSGI) or `root_path` (ASGI).52* Use a given client address for requests by setting `remote_addr` (WSGI) or `client` (ASGI).Drop `raise_app_exceptions` Client argument. (#238)Tom Christiebefore adoptionef9fc0f
lines 53–57why, one step back5354For example:5556```python57# 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 58why, one step back58dispatch = httpx.dispatch.WSGIDispatch(app=app, remote_addr="1.2.3.4")Drop `raise_app_exceptions` Client argument. (#238)Tom Christiebefore adoptionef9fc0f
lines 59–61why, one step back59client = httpx.Client(dispatch=dispatch)60```61Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac
lines 62–74why, one step back62## Build Request6364You can use `Client.build_request()` to build a request and65make modifications before sending the request.6667```python68>>> client = httpx.Client()69>>> req = client.build_request("OPTIONS", "https://example.com")70>>> req.url.full_path = "*" # Build an 'OPTIONS *' request for CORS71>>> client.send(r)72<Response [200 OK]>73```74Fix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5
line 75why, one step back75## Specify the version of the HTTP protocolAdd example of specifying HTTP versions (#337)Paul (xobb) Chubatyybefore adoption0c4460e
line 76why, one step back76Fix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5
line 77why, one step back77One 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 78–88why, one step back7879For example:8081```python82h11_client = httpx.Client(http_versions=["HTTP/1.1"])83h11_response = h11_client.get("https://myserver.com")8485h2_client = httpx.Client(http_versions=["HTTP/2"])86h2_response = h2_client.get("https://myserver.com")87```88Documented netrc support (#225)Can Sarıgölbefore adoption21939fd
lines 89–115why, one step back89## .netrc Support9091HTTPX supports .netrc file. In `trust_env=True` cases, if auth parameter is92not defined, HTTPX tries to add auth into request's header from .netrc file.9394As default `trust_env` is true. To set false:95```python96>>> httpx.get('https://example.org/', trust_env=False)97```9899If `NETRC` environment is empty, HTTPX tries to use default files.100(`~/.netrc`, `~/_netrc`)101102To change `NETRC` environment:103```python104>>> import os105>>> os.environ["NETRC"] = "my_default_folder/.my_netrc"106```107108.netrc file content example:109```110machine netrcexample.org111login example-username112password example-password113114...115```Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 116–161why, one step back116117## HTTP Proxying118119HTTPX supports setting up proxies the same way that Requests does via the `proxies` parameter.120For example to forward all HTTP traffic to `http://127.0.0.1:3080` and all HTTPS traffic121to `http://127.0.0.1:3081` your `proxies` config would look like this:122123```python124>>> client = httpx.Client(proxies={125 "http": "http://127.0.0.1:3080",126 "https": "http://127.0.0.1:3081"127})128```129130Proxies can be configured for a specific scheme and host, all schemes of a host,131all hosts for a scheme, or for all requests. When determining which proxy configuration132to use for a given request this same order is used.133134```python135>>> client = httpx.Client(proxies={136 "http://example.com": "...", # Host+Scheme137 "all://example.com": "...", # Host138 "http": "...", # Scheme139 "all": "...", # All140})141>>> client = httpx.Client(proxies="...") # Shortcut for 'all'142```143144!!! warning145 To make sure that proxies cannot read your traffic,146 and even if the proxy_url uses HTTPS, it is recommended to147 use HTTPS and tunnel requests if possible.148149By default `HTTPProxy` will operate as a forwarding proxy for `http://...` requests150and will establish a `CONNECT` TCP tunnel for `https://` requests. This doesn't change151regardless of the `proxy_url` being `http` or `https`.152153Proxies can be configured to have different behavior such as forwarding or tunneling all requests:154155```python156proxy = httpx.HTTPProxy(157 proxy_url="https://127.0.0.1",158 proxy_mode=httpx.HTTPProxyMode.TUNNEL_ONLY159)160client = httpx.Client(proxies=proxy)161Fix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5
line 162why, one step back162# This request will be tunneled instead of forwarded.Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 163–164why, one step back163client.get("http://example.com")164```