requests
The requests module is a simple HTTP library that makes sending HTTP requests and receiving responses easy.
>>> import requests
If the module is not found, you should install it via pip install requests
.
Let’s try making a GET request to Github’s REST API to retrieve user josiahwang’s repos using Python.
>>> response = requests.get(https://api.github.com/users/josiahwang/repos)
>>> type(response)
>>> help(response) # Read the documentation!
>>> print(response.status_code)
>>> print(response.ok)
>>> print(response.text)
>>> type(response.text)
>>> print(response.json())
>>> type(response.json())
Because the response type is JSON, we can parse the response content with .json()
. You process this as a list
or a dict
(depending on the response).
We can also check out the content of the response header (a dict
).
>>> print(response.headers)
>>> print(response.headers["content-type"])
Read the manual for more details about the available attributes/methods for the Response
object.