Complete the code to call an API endpoint using Python's requests library.
import requests response = requests.[1]('https://api.example.com/data') print(response.status_code)
The get method is used to retrieve data from an API endpoint.
Complete the code to extract JSON data from the API response.
import requests response = requests.get('https://api.example.com/data') data = response.[1]() print(data)
The json() method parses the response content as JSON, which is common for API data.
Fix the error in the code to send a POST request with JSON data.
import requests payload = {'name': 'Alice', 'age': 30} response = requests.post('https://api.example.com/users', [1]=payload) print(response.status_code)
Using the json parameter sends the payload as JSON, which APIs expect for POST data.
Fill both blanks to check if the API response was successful and print the JSON content.
import requests response = requests.get('https://api.example.com/data') if response.[1] == [2]: print(response.json())
The status_code attribute holds the HTTP status code. 200 means success.
Fill all three blanks to create a dictionary comprehension that filters API data for items with 'active' status.
data = [{'id': 1, 'status': 'active'}, {'id': 2, 'status': 'inactive'}, {'id': 3, 'status': 'active'}]
active_items = {item['id']: item for item in data if item[1]'status'] [2] [3]
print(active_items)The code checks if item['status'] == 'active' to filter active items.