APIs connect different software systems and share data. Why is it important to keep APIs secure?
Think about what happens if someone bad gets access to your API.
APIs often handle sensitive data and control important functions. If they are not secure, attackers can steal data or disrupt services.
Consider this simplified Python code snippet checking an API key:
def check_api_key(key):
valid_keys = ['abc123', 'def456']
if key in valid_keys:
return 'Access granted'
else:
return 'Access denied'
print(check_api_key('xyz789'))What will this print?
Is 'xyz789' in the list of valid keys?
The key 'xyz789' is not in the list, so the function returns 'Access denied'.
Look at this code snippet that validates an API token:
def validate_token(token):
if token == None:
return False
if token == '':
return False
return True
print(validate_token(''))What is the problem with this validation?
Check what happens when the token is an empty string.
The code returns False for empty strings and None, so it correctly rejects invalid tokens.
Which of these Python code snippets will cause a syntax error when handling an API request?
Look for missing punctuation or keywords in the if statement.
Option B is missing a colon after the if condition, causing a syntax error.
Given this Python code simulating an API response with security filtering:
response = {'user': 'alice', 'password': 'secret', 'token': 'abc123'}
secured_response = {k: v for k, v in response.items() if k != 'password'}
print(len(secured_response))What number will be printed?
Count keys except 'password'.
The original dictionary has 3 keys. The comprehension removes 'password', leaving 2 keys.