Requests - Handling GET Requests
Requests - Handling GET Requests
Section titled “Requests - Handling GET Requests”This chapter focuses on HTTP GET requests, one of the most common methods used to retrieve data from a specified resource. The requests library makes sending GET requests straightforward.
Making a Simple GET Request
Section titled “Making a Simple GET Request”To make a GET request, you use the requests.get() function, passing the URL of the resource you want to fetch.
Example: Fetching User Data
Section titled “Example: Fetching User Data”import requestsimport json # For pretty printing JSON
url = 'https://jsonplaceholder.typicode.com/users/1' # API endpoint for a single user
try: response = requests.get(url) response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
# Accessing response data print(f"Status Code: {response.status_code}") print(f"Content-Type Header: {response.headers['Content-Type']}")
# For JSON responses, use response.json() user_data = response.json() print("\nUser Data (parsed JSON):") # Pretty print the JSON data print(json.dumps(user_data, indent=2))
# For text content (like HTML or plain text), use response.text # print(f"\nRaw Text Content (first 100 chars): {response.text[:100]}...")
except requests.exceptions.HTTPError as http_err: print(f"HTTP error occurred: {http_err}")except requests.exceptions.RequestException as err: print(f"Other error occurred: {err}")Output (Example)
Section titled “Output (Example)”Status Code: 200Content-Type Header: application/json; charset=utf-8
User Data (parsed JSON):{ "id": 1, "name": "Leanne Graham", "username": "Bret", "email": "Sincere@april.biz", "address": { "street": "Kulas Light", "suite": "Apt. 556", "city": "Gwenborough", "zipcode": "92998-3874", "geo": { "lat": "-37.3159", "lng": "81.1496" } }, "phone": "1-770-736-8031 x56442", "website": "hildegard.org", "company": { "name": "Romaguera-Crona", "catchPhrase": "Multi-layered client-server neural-net", "bs": "harness real-time e-markets" }}Key takeaways from the response object:
response.status_code: The HTTP status code (e.g., 200 for OK, 404 for Not Found).
response.headers: A dictionary-like object of response headers.
response.json(): Parses JSON response content into a Python dictionary or list. Raises JSONDecodeError if parsing fails.
response.text: The response content as a Unicode string.
response.content: The response content as bytes (useful for images or other binary files).
Passing URL Parameters
Section titled “Passing URL Parameters”You can pass URL parameters (also known as query strings) to a GET request using the params argument. Provide a dictionary of key-value pairs, and requests will correctly encode and append them to the URL.
Example: Fetching Posts by User ID
Section titled “Example: Fetching Posts by User ID”import requestsimport json
url = 'https://jsonplaceholder.typicode.com/posts'params = { 'userId': 1, # Filter posts for userId = 1 '_limit': 2 # Limit results to 2 posts (some APIs support this)}
try: response = requests.get(url, params=params) response.raise_for_status()
print(f"Request URL: {response.url}") # Shows the full URL with parameters posts_data = response.json() print("\nFiltered Posts Data:") print(json.dumps(posts_data, indent=2))
except requests.exceptions.HTTPError as http_err: print(f"HTTP error occurred: {http_err}")except requests.exceptions.RequestException as err: print(f"Other error occurred: {err}")The params dictionary {'userId': 1, '_limit': 2} will be converted to the query string ?userId=1&_limit=2 and appended to the base URL.
Output (Example)
Section titled “Output (Example)”Request URL: https://jsonplaceholder.typicode.com/posts?userId=1&_limit=2
Filtered Posts Data:[ { "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" }, { "userId": 1, "id": 2, "title": "qui est esse", "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla" }]Practical Applications
Section titled “Practical Applications”GET requests are fundamental for: - Fetching web pages (HTML content). - Consuming RESTful APIs to retrieve data (often in JSON or XML format). - Checking the availability or status of a web resource. - Downloading files where the entire resource is identified by the URL.