@@ -6,8 +6,6 @@ Using a Client instance to make requests will give you HTTP connection pooling,
6
6
will provide cookie persistence, and allows you to apply configuration across
7
7
all outgoing requests.
8
8
9
- A Client instance is equivalent to a Session instance in ` requests ` .
10
-
11
9
!!! hint
12
10
A Client instance is equivalent to a Session instance in ` requests ` .
13
11
@@ -61,10 +59,10 @@ app = Flask(__name__)
61
59
def hello ():
62
60
return " Hello World!"
63
61
64
- client = httpx.Client(app = app)
65
- r = client.get(' http://example/' )
66
- assert r.status_code == 200
67
- assert r.text == " Hello World!"
62
+ with httpx.Client(app = app) as client:
63
+ r = client.get(' http://example/' )
64
+ assert r.status_code == 200
65
+ assert r.text == " Hello World!"
68
66
```
69
67
70
68
For some more complex cases you might need to customize the WSGI or ASGI
@@ -79,7 +77,8 @@ For example:
79
77
``` python
80
78
# Instantiate a client that makes WSGI requests with a client IP of "1.2.3.4".
81
79
dispatch = httpx.dispatch.WSGIDispatch(app = app, remote_addr = " 1.2.3.4" )
82
- client = httpx.Client(dispatch = dispatch)
80
+ with httpx.Client(dispatch = dispatch) as client:
81
+ ...
83
82
```
84
83
85
84
## Build Request
@@ -88,10 +87,12 @@ You can use `Client.build_request()` to build a request and
88
87
make modifications before sending the request.
89
88
90
89
``` python
91
- >> > client = httpx.Client()
92
- >> > req = client.build_request(" OPTIONS" , " https://example.com" )
93
- >> > req.url.full_path = " *" # Build an 'OPTIONS *' request for CORS
94
- >> > client.send(r)
90
+ >> > with httpx.Client() as client:
91
+ ... req = client.build_request(" OPTIONS" , " https://example.com" )
92
+ ... req.url.full_path = " *" # Build an 'OPTIONS *' request for CORS
93
+ ... r = client.send(req)
94
+ ...
95
+ >> > r
95
96
< Response [200 OK ]>
96
97
```
97
98
@@ -102,11 +103,11 @@ One can set the version of the HTTP protocol for the client in case you want to
102
103
For example:
103
104
104
105
``` python
105
- h11_client = httpx.Client(http_versions = [" HTTP/1.1" ])
106
- h11_response = h11_client.get(" https://myserver.com" )
106
+ with httpx.Client(http_versions = [" HTTP/1.1" ]) as h11_client:
107
+ h11_response = h11_client.get(" https://myserver.com" )
107
108
108
- h2_client = httpx.Client(http_versions = [" HTTP/2" ])
109
- h2_response = h2_client.get(" https://myserver.com" )
109
+ with httpx.Client(http_versions = [" HTTP/2" ]) as h2_client:
110
+ h2_response = h2_client.get(" https://myserver.com" )
110
111
```
111
112
112
113
## .netrc Support
@@ -149,24 +150,31 @@ For example to forward all HTTP traffic to `http://127.0.0.1:3080` and all HTTPS
149
150
to ` http://127.0.0.1:3081 ` your ` proxies ` config would look like this:
150
151
151
152
``` python
152
- >> > client = httpx.Client(proxies = {
153
- " http" : " http://127.0.0.1:3080" ,
154
- " https" : " http://127.0.0.1:3081"
155
- })
153
+ >> > proxies = {
154
+ ... " http" : " http://127.0.0.1:3080" ,
155
+ ... " https" : " http://127.0.0.1:3081"
156
+ ... }
157
+ >> > with httpx.Client(proxies = proxies) as client:
158
+ ... ...
156
159
```
157
160
158
161
Proxies can be configured for a specific scheme and host, all schemes of a host,
159
162
all hosts for a scheme, or for all requests. When determining which proxy configuration
160
163
to use for a given request this same order is used.
161
164
162
165
``` python
163
- >> > client = httpx.Client(proxies = {
164
- " http://example.com" : " ..." , # Host+Scheme
165
- " all://example.com" : " ..." , # Host
166
- " http" : " ..." , # Scheme
167
- " all" : " ..." , # All
168
- })
169
- >> > client = httpx.Client(proxies = " ..." ) # Shortcut for 'all'
166
+ >> > proxies = {
167
+ ... " http://example.com" : " ..." , # Host+Scheme
168
+ ... " all://example.com" : " ..." , # Host
169
+ ... " http" : " ..." , # Scheme
170
+ ... " all" : " ..." , # All
171
+ ... }
172
+ >> > with httpx.Client(proxies = proxies) as client:
173
+ ... ...
174
+ ...
175
+ >> > proxy = " ..." # Shortcut for {'all': '...'}
176
+ >> > with httpx.Client(proxies = proxy) as client:
177
+ ... ...
170
178
```
171
179
172
180
!!! warning
@@ -185,10 +193,9 @@ proxy = httpx.HTTPProxy(
185
193
proxy_url = " https://127.0.0.1" ,
186
194
proxy_mode = httpx.HTTPProxyMode.TUNNEL_ONLY
187
195
)
188
- client = httpx.Client(proxies = proxy)
189
-
190
- # This request will be tunneled instead of forwarded.
191
- client.get(" http://example.com" )
196
+ with httpx.Client(proxies = proxy) as client:
197
+ # This request will be tunneled instead of forwarded.
198
+ r = client.get(" http://example.com" )
192
199
```
193
200
194
201
@@ -213,15 +220,15 @@ You can set timeouts on two levels:
213
220
httpx.get(' http://example.com/api/v1/example' , timeout = 5 )
214
221
215
222
# Or, with a client:
216
- client = httpx.Client()
217
- client.get(" http://example.com/api/v1/example" , timeout = 5 )
223
+ with httpx.Client() as client:
224
+ client.get(" http://example.com/api/v1/example" , timeout = 5 )
218
225
```
219
226
220
227
- On a client instance, which results in the given ` timeout ` being used as a default for requests made with this client:
221
228
222
229
``` python
223
- client = httpx.Client(timeout = 5 )
224
- client.get(' http://example.com/api/v1/example' )
230
+ with httpx.Client(timeout = 5 ) as client:
231
+ client.get(' http://example.com/api/v1/example' )
225
232
```
226
233
227
234
Besides, you can pass timeouts in two forms:
@@ -252,8 +259,8 @@ url = "http://example.com/api/v1/delay/10"
252
259
httpx.get(url, timeout = None ) # Times out after 5s
253
260
254
261
255
- client = httpx.Client(timeout = None )
256
- client.get(url) # Does not timeout, returns after 10s
262
+ with httpx.Client(timeout = None ) as client:
263
+ client.get(url) # Does not timeout, returns after 10s
257
264
258
265
259
266
timeout = httpx.TimeoutConfig(
0 commit comments