The request() method of HTTPConnection in Python

Method Name:

request

Method Signature:

request(method, url, body=None, headers={}, *, encode_chunked=False)

Parameters:

method – The HTTP method to be used. e.g, GET, POST, PUT and others.

url – The URL from which the contents/resource to be fetched from the server.

headers – The headers of the HTTP message. They could be general headers, request headers, response headers or entity headers.

body – The body of the HTTP message. This parameter is used for HTTP requests like PUT.

encode_chunked - The parameter to indicate that the data transfer is using the chunked encoding. The default value is False.

Return Value:

None. The HTTP response is obtained by calling the method getresponse() on the HTTPConnection instance.

Overview:

  • The method request() of HTTPConnection class represents a HTTP request.
  • A HTTP request can ask for a web page from, send data to and write data to the web server. Hence, the request() method has the parameters headers and body that represent the parts of a HTTP message.

Example – HTTP get request:

The Python example code below, creates a HTTPConnection instance and sends a HTTP request GET through the connection. The resource/HTML from the server is obtained by reading the response obtained through the method getresponse().

# Example Python program that requests a web page using the

# HTTPConnection.request() method

import http.client as hc

 

httpHost    = "example.com";

httpMethod  = "GET";

url         = "/";

headers     = {"Accept: text/html"};

 

# Connect to a HTTP server

hcon = hc.HTTPConnection(httpHost);

 

# Request for a URL

hcon.request(httpMethod, url);

 

# Get the HTTP response

response = hcon.getresponse();

 

# Read the HTTP response

html = response.read();

 

# Print HTML

print(html);

 

Output:

b'<!doctype html>\n<html>\n<head>\n    <title>Example Domain</title>\n\n    <meta charset="utf-8" />\n    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />\n    <meta name="viewport" content="width=device-width, initial-scale=1" />\n    <style type="text/css">\n    body {\n        background-color: #f0f0f2;\n        margin: 0;\n        padding: 0;\n        font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;\n        \n    }\n    div {\n        width: 600px;\n        margin: 5em auto;\n        padding: 50px;\n        background-color: #fff;\n        border-radius: 1em;\n    }\n    a:link, a:visited {\n        color: #38488f;\n        text-decoration: none;\n    }\n    @media (max-width: 700px) {\n        body {\n            background-color: #fff;\n        }\n        div {\n            width: auto;\n            margin: 0 auto;\n            border-radius: 0;\n            padding: 1em;\n        }\n    }\n    </style>    \n</head>\n\n<body>\n<div>\n    <h1>Example Domain</h1>\n    <p>This domain is established to be used for illustrative examples in documents. You may use this\n    domain in examples without prior coordination or asking for permission.</p>\n    <p><a href="http://www.iana.org/domains/example">More information...</a></p>\n</div>\n</body>\n</html>\n'


Copyright 2024 © pythontic.com