You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -504,7 +504,7 @@ The `NetRCAuth()` class uses [the `netrc.netrc()` function from the Python stand
504
504
505
505
## HTTP Proxying
506
506
507
-
HTTPX supports setting up [HTTP proxies](https://en.wikipedia.org/wiki/Proxy_server#Web_proxy_servers) via the `proxies` parameter to be passed on client initialization or top-level API functions like `httpx.get(..., proxies=...)`.
507
+
HTTPX supports setting up [HTTP proxies](https://en.wikipedia.org/wiki/Proxy_server#Web_proxy_servers) via the `proxy` parameter to be passed on client initialization or top-level API functions like `httpx.get(..., proxy=...)`.
@@ -516,19 +516,19 @@ HTTPX supports setting up [HTTP proxies](https://en.wikipedia.org/wiki/Proxy_ser
516
516
To route all traffic (HTTP and HTTPS) to a proxy located at `http://localhost:8030`, pass the proxy URL to the client...
517
517
518
518
```python
519
-
with httpx.Client(proxies="http://localhost:8030") as client:
519
+
with httpx.Client(proxy="http://localhost:8030") as client:
520
520
...
521
521
```
522
522
523
-
For more advanced use cases, pass a proxies`dict`. For example, to route HTTP and HTTPS requests to 2 different proxies, respectively located at `http://localhost:8030`, and `http://localhost:8031`, pass a `dict` of proxy URLs:
523
+
For more advanced use cases, pass a mounts`dict`. For example, to route HTTP and HTTPS requests to 2 different proxies, respectively located at `http://localhost:8030`, and `http://localhost:8031`, pass a `dict` of proxy URLs:
HTTPX provides fine-grained controls for deciding which requests should go through a proxy, and which shouldn't. This process is known as proxy routing.
558
-
559
-
The `proxies` dictionary maps URL patterns ("proxy keys") to proxy URLs. HTTPX matches requested URLs against proxy keys to decide which proxy should be used, if any. Matching is done from most specific proxy keys (e.g. `https://<domain>:<port>`) to least specific ones (e.g. `https://`).
560
-
561
-
HTTPX supports routing proxies based on **scheme**, **domain**, **port**, or a combination of these.
562
-
563
-
#### Wildcard routing
564
-
565
-
Route everything through a proxy...
566
-
567
-
```python
568
-
proxies = {
569
-
"all://": "http://localhost:8030",
570
-
}
571
-
```
572
-
573
-
#### Scheme routing
574
-
575
-
Route HTTP requests through one proxy, and HTTPS requests through another...
576
-
577
-
```python
578
-
proxies = {
579
-
"http://": "http://localhost:8030",
580
-
"https://": "http://localhost:8031",
581
-
}
582
-
```
583
-
584
-
#### Domain routing
585
-
586
-
Proxy all requests on domain "example.com", let other requests pass through...
587
-
588
-
```python
589
-
proxies = {
590
-
"all://example.com": "http://localhost:8030",
591
-
}
592
-
```
593
-
594
-
Proxy HTTP requests on domain "example.com", let HTTPS and other requests pass through...
595
-
596
-
```python
597
-
proxies = {
598
-
"http://example.com": "http://localhost:8030",
599
-
}
600
-
```
601
-
602
-
Proxy all requests to "example.com" and its subdomains, let other requests pass through...
603
-
604
-
```python
605
-
proxies = {
606
-
"all://*example.com": "http://localhost:8030",
607
-
}
608
-
```
609
-
610
-
Proxy all requests to strict subdomains of "example.com", let "example.com" and other requests pass through...
611
-
612
-
```python
613
-
proxies = {
614
-
"all://*.example.com": "http://localhost:8030",
615
-
}
616
-
```
617
-
618
-
#### Port routing
619
-
620
-
Proxy HTTPS requests on port 1234 to "example.com"...
@@ -1294,3 +1172,125 @@ Adding support for custom schemes:
1294
1172
mounts = {"file://": FileSystemTransport()}
1295
1173
client = httpx.Client(mounts=mounts)
1296
1174
```
1175
+
1176
+
### Routing
1177
+
1178
+
HTTPX provides a powerful mechanism for routing requests, allowing you to write complex rules that specify which transport should be used for each request.
1179
+
1180
+
The `mounts` dictionary maps URL patterns to HTTP transports. HTTPX matches requested URLs against URL patterns to decide which transport should be used, if any. Matching is done from most specific URL patterns (e.g. `https://<domain>:<port>`) to least specific ones (e.g. `https://`).
1181
+
1182
+
HTTPX supports routing requests based on **scheme**, **domain**, **port**, or a combination of these.
When using `httpx.Client(proxies={...})` to map to a selection of different proxies, we use full URL schemes, such as `proxies={"http://": ..., "https://": ...}`.
160
+
HTTPX uses the mounts argument for HTTP proxying and transport routing.
161
+
It can do much more than proxies and allows you to configure more than just the proxy route.
162
+
For more detailed documentation, see [Mounting Transports](advanced.md#mounting-transports).
163
+
164
+
When using `httpx.Client(mounts={...})` to map to a selection of different transports, we use full URL schemes, such as `mounts={"http://": ..., "https://": ...}`.
161
165
162
166
This is different to the `requests` usage of `proxies={"http": ..., "https": ...}`.
163
167
164
-
This change is for better consistency with more complex mappings, that might also include domain names, such as `proxies={"all://": ..., "all://www.example.com": None}` which maps all requests onto a proxy, except for requests to "www.example.com" which have an explicit exclusion.
168
+
This change is for better consistency with more complex mappings, that might also include domain names, such as `mounts={"all://": ..., httpx.HTTPTransport(proxy="all://www.example.com": None})` which maps all requests onto a proxy, except for requests to "www.example.com" which have an explicit exclusion.
165
169
166
-
Also note that `requests.Session.request(...)` allows a `proxies=...` parameter, whereas `httpx.Client.request(...)` does not.
170
+
Also note that `requests.Session.request(...)` allows a `proxies=...` parameter, whereas `httpx.Client.request(...)` does not allow `mounts=...`.
167
171
168
172
## SSL configuration
169
173
@@ -195,7 +199,7 @@ We don't support `response.is_ok` since the naming is ambiguous there, and might
195
199
196
200
There is no notion of [prepared requests](https://requests.readthedocs.io/en/stable/user/advanced/#prepared-requests) in HTTPX. If you need to customize request instantiation, see [Request instances](advanced.md#request-instances).
197
201
198
-
Besides, `httpx.Request()` does not support the `auth`, `timeout`, `follow_redirects`, `proxies`, `verify` and `cert` parameters. However these are available in `httpx.request`, `httpx.get`, `httpx.post` etc., as well as on [`Client` instances](advanced.md#client-instances).
202
+
Besides, `httpx.Request()` does not support the `auth`, `timeout`, `follow_redirects`, `mounts`, `verify` and `cert` parameters. However these are available in `httpx.request`, `httpx.get`, `httpx.post` etc., as well as on [`Client` instances](advanced.md#client-instances).
0 commit comments