Python HTTP Requests
Python can use a module called requests to send HTTP commands to a server and process the response. This is similar to using curl or postman. This is especially useful when accessing a RESTful API.
Installation
The requests module is not part of Python by default. It can be installed with pip:
C:\>pip install requests Collecting requests Downloading requests-2.26.0-py2.py3-none-any.whl (62 kB) |████████████████████████████████| 62 kB 330 kB/s Collecting idna<4,>=2.5 Downloading idna-3.3-py3-none-any.whl (61 kB) |████████████████████████████████| 61 kB 4.1 MB/s Collecting certifi>=2017.4.17 Downloading certifi-2021.10.8-py2.py3-none-any.whl (149 kB) |████████████████████████████████| 149 kB 6.4 MB/s Collecting urllib3<1.27,>=1.21.1 Downloading urllib3-1.26.7-py2.py3-none-any.whl (138 kB) |████████████████████████████████| 138 kB 6.4 MB/s Collecting charset-normalizer~=2.0.0 Downloading charset_normalizer-2.0.9-py3-none-any.whl (39 kB) Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests WARNING: The script normalizer.exe is installed in 'C:\Users\admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. Successfully installed certifi-2021.10.8 charset-normalizer-2.0.9 idna-3.3 requests-2.26.0 urllib3-
Before using requests, it must be imported with import requests.
GET Requests
GET commands are very easy. In the example on the right, we make a simple GET request to an unauthenticated API, publically available on the internet.
We use requests to send a get to the API listed in the URL variable. The server’s response is stored in the response variable.
The final line just parses the result to pull out a single piece of data. You could also use response.json() to print the entire response, or response.content for XML.
>>> import requests >>> url = "https://api.punkapi.com/v2/beers" >>> response = requests.get(url) >>> response.json()[1]['name'] 'Trashy Blonde'
response.status_code returns the HTTP status code. Often you would want to see ‘200’ as the response.
>>> response.status_code 200
response.headers will show all the headers that the server sent back.
>>> response.headers {'Content-Type': 'application/json; charset=utf-8', 'Connection': 'keep-alive', 'strict-transport-security': 'max-age=15552000; includeSubDomains', 'x-frame-options': 'SAMEORIGIN', 'x-content-type-options': 'nosniff'}