Method Name:
getresponse
Method Signature:
getresponse()
Parameters:
None
Return Value:
An object of type HTTPResponse.
Overview:
- The method getresponse() returns an HTTPResponse object which has the response data for the previous HTTP request.
- Once an HTTPResponse object is obtained, the read() method is called on the object to read the actual resource received from the server.
Example:
# Example Python Program that uses HTTPConnection.getresponse() # to get the response for a request sent using HTTPConnection.request() import http.client as hc
httpmethod = "POST"; urlToFetch = "/post";
# POST data body = "Message=Hello&Destination=World";
# Create a HTTP Connection httpcon = hc.HTTPConnection("httpbin.org");
# Send the HTTP post request to the server httpcon.request(httpmethod, urlToFetch, body);
# Get the HTTP response httpResponse = httpcon.getresponse();
# Read the HTTP response resourceFetched = httpResponse.read();
# Print the response print(resourceFetched); |
Output:
b'{\n "args": {}, \n "data": "Message=Hello&Destination=World", \n "files": {}, \n "form": {}, \n "headers": {\n "Accept-Encoding": "identity", \n "Content-Length": "31", \n "Host": "httpbin.org"\n }, \n "json": null, \n "origin": "106.200.230.161, 106.200.230.161", \n "url": "https://httpbin.org/post"\n}\n' |