Python (Scripting) - GET request using pycurl

by
Jeremy Canfield |
Updated: November 08 2024
| Python (Scripting) articles
Almost always, the requests module is be used to issue a REST API request.
- DELETE request
- GET request
- PATCH request (typically used to update an existing resource)
- POST request (typically used to create a new resource)
- PUT request (typically used to update an existing resource)
However, if for whatever reason requests cannot be used, pycurl can get used.
You will probably need to use pip to install the pycurl module.
pip install pycurl
And here is an example of how to submit a GET request to www.example.com and to display the response data.
#!/usr/bin/python3
import pycurl
import certifi
from io import BytesIO
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, 'http://www.example.com/')
c.setopt(c.WRITEDATA, buffer)
c.setopt(c.CAINFO, certifi.where())
c.perform()
c.close()
body = buffer.getvalue()
print(body.decode('iso-8859-1'))
Did you find this article helpful?
If so, consider buying me a coffee over at