Skip to content
Myr

advanced.md

This file

Path

docs/advanced.md

71 lines · 1.6 KiB · Markdown

Last changed by

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

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

lines 44–71why, one step back
44
45## .netrc Support4647HTTPX supports .netrc file. In `trust_env=True` cases, if auth parameter is48not defined, HTTPX tries to add auth into request's header from .netrc file.4950As default `trust_env` is true. To set false:51```python52>>> httpx.get('https://example.org/', trust_env=False)53```5455If `NETRC` environment is empty, HTTPX tries to use default files.56(`~/.netrc`, `~/_netrc`)5758To change `NETRC` environment:59```python60>>> import os61>>> os.environ["NETRC"] = "my_default_folder/.my_netrc"62```6364.netrc file content example:65```66machine netrcexample.org67login example-username68password example-password6970...71```