Example: GET Request - 2.2 | Chapter 12: Working with External Libraries and APIs | Python Advance
K12 Students

Academics

AI-Powered learning for Grades 8–12, aligned with major Indian and international curricula.

Academics
Professionals

Professional Courses

Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.

Professional Courses
Games

Interactive Games

Fun, engaging games to boost memory, math fluency, typing speed, and English skillsβ€”perfect for learners of all ages.

games

Raises HTTPError if not 2xx

except requests.exceptions.RequestException as e:
print("Error:", e)


Short Summary
A GET request is used to retrieve data from a server. Python's requests.get() lets you make such requests and read the response, which can be in text, JSON, or other formats.

πŸ“– Medium Summary
The GET method is one of the most commonly used HTTP methods. It’s used to request data from a specified resource, without changing anything on the server. Query parameters can be attached using the params argument. The response usually contains the requested data, and can be parsed using .json() or .text().

Example:

python

import requests

response = requests.get("https://jsonplaceholder.typicode.com/posts/1")
data = response.json()
print(data["title"])
πŸ“š Long Summary
πŸ”Έ What is a GET Request?
A GET request asks the server to send back data.

It is safe and idempotent (doesn’t change the server state).

Used in REST APIs to fetch data.

πŸ”Έ Syntax and Parameters
python
C
requests.get(url, params=None, headers=None, timeout=None)
params: Dictionary of URL query parameters

headers: Custom HTTP headers (e.g., auth tokens)

timeout: Optional timeout in seconds

πŸ”Έ Example with Query Parameters
python

import requests

payload = {"userId": 1}
response = requests.get("https://jsonplaceholder.typicode.com/posts", params=payload)

if response.status_code == 200:
posts = response.json()
for post in posts:
print(post["title"])
πŸ”Έ Error Handling
python

try:
response = requests.get("https://api.example.com/data", timeout=5)
response.raise_for_status() # Raises HTTPError if not 2xx
except requests.exceptions.RequestException as e:
print("Error:", e)