Requests

Author

Calvin, in a panic

requests

Installing the requests library

  • I recommend the use of install script.
  • This is the example we used at the beginning to install pillow which is how PGL makes images.
import sys, subprocess; subprocess.run([sys.executable, '-m', 'pip', 'install', '--user', 'pillow'])
  • Now, that is ugly, so we’ll adjust the lines a little bit and change pillow to requests
import sys, subprocess

subprocess.run(
    [
        sys.executable, 
        '-m', 
        'pip', 
        'install', 
        '--break-system-packages', 
        'requests'
    ]
)
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: requests in /usr/lib/python3/dist-packages (2.31.0)
CompletedProcess(args=['/usr/bin/python3', '-m', 'pip', 'install', '--break-system-packages', 'requests'], returncode=0)
  • Once you run that script, you should confirm you have requests installed.
  • Do so by:
    1. Create a Python file.
    2. Include import requests
    3. See if it explodes.
import requests

print(requests)
<module 'requests' from '/usr/lib/python3/dist-packages/requests/__init__.py'>

Comparing GET vs. POST

Here are some places to read more: - G4G - Differences between GET and POST - G4G - GET POST requests - Requests

Example

r = requests.get('https://api.github.com/user', auth=('user', 'pass'))

r
<Response [401]>
r = requests.get('https://cd-public.github.io/scicom/helium.json')

r
<Response [200]>
r.status_code
200
r.headers
{'Connection': 'keep-alive', 'Content-Length': '177', 'Server': 'GitHub.com', 'Content-Type': 'application/json; charset=utf-8', 'Last-Modified': 'Wed, 12 Nov 2025 19:21:05 GMT', 'Access-Control-Allow-Origin': '*', 'Strict-Transport-Security': 'max-age=31556952', 'ETag': '"6914de21-b1"', 'expires': 'Thu, 20 Nov 2025 23:08:05 GMT', 'Cache-Control': 'max-age=600', 'x-proxy-cache': 'MISS', 'X-GitHub-Request-Id': '5E8A:30E453:17108A:178109:691F9CFD', 'Accept-Ranges': 'bytes', 'Age': '0', 'Date': 'Thu, 20 Nov 2025 22:58:05 GMT', 'Via': '1.1 varnish', 'X-Served-By': 'cache-pdx12330-PDX', 'X-Cache': 'MISS', 'X-Cache-Hits': '0', 'X-Timer': 'S1763679486.885917,VS0,VE86', 'Vary': 'Accept-Encoding', 'X-Fastly-Request-ID': '60471c57d08c4b99217f49e3ea1c59cfde0d7db5'}
r.headers['content-type']
'application/json; charset=utf-8'
r.encoding # I can use emojis
'utf-8'
txt = r.text

print(txt)
print(type(txt))
print(txt[0])
{
    "symbol": "He",
    "phase_stp": "gas",
    "group": 18,
    "period": 1,
    "boiling_point": {
        "K": 4.222,
        "C": -268.928,
        "F": -452.070
     }
}

<class 'str'>
{
js = r.json()

print(js)
print(type(js))
print(js['symbol'])
{'symbol': 'He', 'phase_stp': 'gas', 'group': 18, 'period': 1, 'boiling_point': {'K': 4.222, 'C': -268.928, 'F': -452.07}}
<class 'dict'>
He

When you get something off the internet with requests, there is going to be “stuff” in it, and the stuff could be JSON or text (or any image) - but you can ask the request by looking up values of keys in the Python dictionary that is returned by the request.

How to add a payload to a POST request

\(\exists\) a way to post things to the internet from Python. It will use requests.post

  • This is “hard” because there has to be a remote server will to accept your request.
    • This harder than having a remote server that is just hosting a file.

How to check the returned status of a request

  • This one is easy.
r.status_code

How to convert a returned request to JSON to extract various returned keys

  • This one is super easy as well!
r.json()