Skip to content
Myr

advanced.md

This file

Path

docs/advanced.md

164 lines · 4.6 KiB · Markdown

Last changed by

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

History of this file

Take it

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

lines 1–8why, 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.8

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 9–10why, one step back
9A Client instance is equivalent to a Session instance in `requests`.
10

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

line 11why, one step back
11```python

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

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

lines 13–19why, one step back
13>>> r = client.get('https://example.org/')
14>>> r15<Response [200 OK]>16```1718## Calling into Python Web Apps19

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

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

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

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

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 32why, one step back
32import httpx

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

lines 33–40why, one step back
33
3435app = Flask(__name__)3637@app.route("/")38def hello():39 return "Hello World!"40

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

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

lines 42–45why, one step back
42r = 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 back
46

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

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

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

lines 48–57why, one step back
48dispatch. This allows you to:
4950* 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 the client address for requests, by setting `remote_addr` (WSGI) or `client` (ASGI).5354For 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 back
58dispatch = 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 back
59client = httpx.Client(dispatch=dispatch)
60```61

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

lines 62–74why, one step back
62## Build Request
6364You 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```74

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

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

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

line 76why, one step back
76

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

line 77why, one step back
77One 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 back
78
79For 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```88

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

lines 89–115why, one step back
89## .netrc Support
9091HTTPX 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 back
116
117## 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)161

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

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

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

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