Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.
Fun, engaging games to boost memory, math fluency, typing speed, and English skillsβperfect for learners of all ages.
Enroll to start learning
Youβve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take mock test.
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)