Reputation: 325
I am interested in retrieving the response code, body and HTTP headers using urllib3. The previous code which I had written was in Python 2 and now I have to rewrite it for Python 3.
import urllib3
http = urllib3.PoolManager();
response = http.request('GET', 'http://192.168.43.131:8000')
print(response)
I tried different sources, but can someone point me in the right direction of the give a few pointers?
Upvotes: 3
Views: 4834
Reputation: 178
I think you mean this:
import json
import urllib3 # pip install urllib3
http = urllib3.PoolManager()
response = http.request('GET', 'http://192.168.43.131:8000')
print(response.status)
print(response.headers)
print(json.loads(response.data.decode('utf-8'))) # body
Convert body to json with orjson:
import orjson # pip install orjson
import urllib3 # pip install urllib3
response = http.request('GET', 'http://192.168.43.131:8000')
print(orjson.loads(response.data)) # body
doc: https://urllib3.readthedocs.io/en/latest/user-guide.html
Upvotes: 2
Reputation: 364
I assume you have found this?
https://urllib3.readthedocs.io/en/latest/user-guide.html#response-content
That would suggest you could get the status code, for example, with
print(response.status)
Upvotes: 0