Bird
Raised Fist0

Consider this Python code snippet using the requests library to call an API with an API key:

medium📝 Predict Output Q13 of Q15
Rest API - Authentication and Authorization
Consider this Python code snippet using the requests library to call an API with an API key:
import requests
headers = {"X-API-KEY": "12345"}
response = requests.get("https://api.example.com/data", headers=headers)
print(response.status_code)
What will this code print if the API key is valid and the request succeeds?
A401
B200
C404
D500
Step-by-Step Solution
Solution:
  1. Step 1: Understand HTTP status codes

    200 means success, 401 means unauthorized, 404 means not found, 500 means server error.
  2. Step 2: Analyze the code behavior with valid API key

    With a valid API key, the server should authorize the request and respond with 200.
  3. Final Answer:

    200 -> Option B
  4. Quick Check:

    Valid key = 200 OK [OK]
Quick Trick: Valid API key means HTTP 200 success code [OK]
Common Mistakes:
MISTAKES
  • Confusing 401 Unauthorized with success
  • Assuming 404 means invalid key
  • Thinking 500 is related to API key

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes