Problem Formulation: How to perform an HTTP get call in Python?

Solution: Use Python’s requests
library.
import requests payload = {'param_1': 'value_1', 'param_2': 'value_2'} r = requests.get('http://example.com/', params=payload)
This is semantically equivalent to issuing an HTTP get call:
http://example.com?param_1=value_1¶m_2=value_2
In fact, you can obtain this exact URL by using the r.url
attribute on the request object:
print(r.url) # http://example.com?param_1=value_1¶m_2=value_2
You can find the text response by using the r.text attribute of the request object:
print(r.text) # [... return value from server ...]
Alternatively, if you expect a json
object, you can also do:
print(r.json()) # [{... returned json object ... }]
Try it yourself in our interactive Jupyter Notebook with Google Colab:
Resources:
- https://2.python-requests.org/en/master/api/#requests.Response
- https://2.python-requests.org/en/master/user/quickstart/#passing-parameters-in-urls
- https://stackoverflow.com/questions/5767464/what-is-the-syntax-for-adding-a-get-parameter-to-a-url
- https://stackoverflow.com/questions/50737866/python-requests-pass-parameter-via-get

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.