Skip to content

Commit 7ba6288

Browse files
davidfstrmerwok
andauthored
gh-102327: Extend docs for "url" and "headers" parameters to HTTPConnection.request()
Added example on how to use the HTTPConnection object for making GET request. Original issue: #102327 --------- Co-authored-by: Éric <[email protected]>
1 parent 01c321c commit 7ba6288

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

Doc/library/http.client.rst

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,10 @@ HTTPConnection Objects
264264
encode_chunked=False)
265265

266266
This will send a request to the server using the HTTP request
267-
method *method* and the selector *url*.
267+
method *method* and the request URI *url*. The provided *url* must be
268+
an absolute path to conform with :rfc:`RFC 2616 §5.1.2 <2616#section-5.1.2>`
269+
(unless connecting to an HTTP proxy server or using the ``OPTIONS`` or
270+
``CONNECT`` methods).
268271

269272
If *body* is specified, the specified data is sent after the headers are
270273
finished. It may be a :class:`str`, a :term:`bytes-like object`, an
@@ -279,7 +282,10 @@ HTTPConnection Objects
279282
iterable are sent as is until the iterable is exhausted.
280283

281284
The *headers* argument should be a mapping of extra HTTP headers to send
282-
with the request.
285+
with the request. A :rfc:`Host header <2616#section-14.23>`
286+
must be provided to conform with :rfc:`RFC 2616 §5.1.2 <2616#section-5.1.2>`
287+
(unless connecting to an HTTP proxy server or using the ``OPTIONS`` or
288+
``CONNECT`` methods).
283289

284290
If *headers* contains neither Content-Length nor Transfer-Encoding,
285291
but there is a request body, one of those
@@ -298,6 +304,16 @@ HTTPConnection Objects
298304
HTTPConnection object assumes that all encoding is handled by the
299305
calling code. If it is ``True``, the body will be chunk-encoded.
300306

307+
For example, to perform a ``GET`` request to ``https://docs.python.org/3/``::
308+
309+
>>> import http.client
310+
>>> host = "docs.python.org"
311+
>>> conn = http.client.HTTPSConnection(host)
312+
>>> conn.request("GET", "/3/", headers={"Host": host})
313+
>>> response = conn.getresponse()
314+
>>> print(response.status, response.reason)
315+
200 OK
316+
301317
.. note::
302318
Chunked transfer encoding has been added to the HTTP protocol
303319
version 1.1. Unless the HTTP server is known to handle HTTP 1.1,

0 commit comments

Comments
 (0)