Jotting one man's journey through software development, programming, and technology
◀️ Home
requestsrequests.get() is used to send an HTTP GET request to a specified URL and retrieve the response.
import requests
response = requests.get("https://api.github.com")
print(response.status_code) # HTTP status code (e.g., 200, 404)
print(response.text) # Response body as a string
print(response.json()) # Parse JSON response (if applicable)
paramsYou can pass query parameters using the params argument. Used to send data in the URL’s query string. Commonly used in API requests to filter or modify responses and sent as key-value pairs.
Use Cases: Pagination, filtering, search queries, API authentication (sometimes).
# Example: Fetching paginated data
import requests
url = "https://api.example.com/items"
params = {"page": 2, "limit": 10} # Query parameters
response = requests.get(url, params=params)
print(response.url) # Shows: https://api.example.com/items?page=2&limit=10
headersUsed to send additional metadata with the request. Typically includes authentication tokens, content types, and custom headers.
Use Cases: Authentication, setting content types (JSON, XML), customizing requests.
# Example: Sending an API key in the headers
headers = {
"Authorization": "Bearer YOUR_ACCESS_TOKEN",
"Accept": "application/json"
}
response = requests.get("https://api.example.com/protected", headers=headers)
Headers follow a general structure, but the specific headers required depend on the website or API.
Common Headers (Used Across Most APIs/Websites):
{"Authorization": "Bearer YOUR_ACCESS_TOKEN"}
{"Content-Type": "application/json"} # JSON payload
{"Content-Type": "application/x-www-form-urlencoded"} # Form data
{"User-Agent": "Mozilla/5.0"} # Spoof browser requests
{"Accept": "application/json"}