advanced.md
This file
Path
docs/advanced.md
86 lines · 2.2 KiB · Markdown
Last changed by
Drop `raise_app_exceptions` Client argument. (#238)Tom Christiebefore adoptionef9fc0f
History of this fileTake it
Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 1–9why, 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.89```pythonRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 10why, one step back10>>> client = httpx.Client()Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 11–17why, one step back11>>> r = client.get('https://example.org/')12>>> r13<Response [200 OK]>14```1516## Calling into Python Web Apps17Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 18why, one step back18You can configure an `httpx` client to call directly into a Python webSupport WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 19–22why, one step back19application, using either the WSGI or ASGI protocol.2021This is particularly useful for two main use-cases:22Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 23why, one step back23* Using `httpx` as a client, inside test cases.Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 24–29why, one step back24* 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 FlaskRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 30why, one step back30import httpxSupport WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 31–38why, one step back313233app = Flask(__name__)3435@app.route("/")36def hello():37 return "Hello World!"38Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 39why, one step back39client = httpx.Client(app=app)Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 40–43why, one step back40r = 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 back44Drop `raise_app_exceptions` Client argument. (#238)Tom Christiebefore adoptionef9fc0f
lines 45–59why, one step back45For some more complex cases you might need to customize the WSGI or ASGI46dispatch. 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".56dispatch = httpx.WSGIDispatch(app=app, remote_addr="1.2.3.4")57client = httpx.Client(dispatch=dispatch)58```59Documented netrc support (#225)Can Sarıgölbefore adoption21939fd
lines 60–86why, one step back60## .netrc Support6162HTTPX supports .netrc file. In `trust_env=True` cases, if auth parameter is63not defined, HTTPX tries to add auth into request's header from .netrc file.6465As default `trust_env` is true. To set false:66```python67>>> httpx.get('https://example.org/', trust_env=False)68```6970If `NETRC` environment is empty, HTTPX tries to use default files.71(`~/.netrc`, `~/_netrc`)7273To change `NETRC` environment:74```python75>>> import os76>>> os.environ["NETRC"] = "my_default_folder/.my_netrc"77```7879.netrc file content example:80```81machine netrcexample.org82login example-username83password example-password8485...86```