cannot make requests wit urllib3

48 Views Asked by At

Can anyone provide a working urllib3 example that works? I have tried the example on the website https://pypi.org/project/urllib3/:

import urllib3
resp = urllib3.request("GET", "http://httpbin.org/robots.txt")

but keep on getting this error (using Python 3.10):

AttributeError: module 'urllib3.request' has no attribute 'Request'
1

There are 1 best solutions below

1
Jeevan ebi On

The correct way to use urllib3 to send HTTP requests is slightly different

import urllib3

# Create a PoolManager instance
http = urllib3.PoolManager()

# Send a GET request to http://httpbin.org/robots.txt
response = http.request("GET", "http://httpbin.org/robots.txt")


print("Status code:", response.status)
print("Response data:")
print(response.data.decode("utf-8"))  # Decode response data as UTF-8 and print

Make sure to install urllib3 if you haven't already by running pip install urllib3.

This code should work without errors and retrieve the robots.txt file from http://httpbin.org/robots.txt.

Ref: https://urllib3.readthedocs.io/en/stable/user-guide.html