import requests # URL of the API endpoint url = "https://jsonplaceholder.typicode.com/posts" # Example API that returns a list of posts try: # Send a GET request response = requests.get(url) # Check if the response is successful response.raise_for_status() # Raises HTTPError for bad responses (4xx and 5xx) # Parse the JSON response data = response.json() if isinstance(data, list): # Check if the response is a list print("Received a list with", len(data), "items:") for item in data[:5]: # Display the first 5 items as an example print(item) else: print("The response is not a list:", data) except requests.exceptions.RequestException as e: print("An error occurred:", e)