Bird
0
0

Given this Python code snippet using the requests library, what will be the output if the API key is invalid?

medium📝 Predict Output Q4 of 15
Rest API - Authentication and Authorization
Given this Python code snippet using the requests library, what will be the output if the API key is invalid?
import requests
headers = {"Authorization": "ApiKey wrong_key"}
response = requests.get("https://api.example.com/data", headers=headers)
print(response.status_code)
A200
B500
C404
D401
Step-by-Step Solution
Solution:
  1. Step 1: Understand HTTP status codes for authentication

    401 means unauthorized access due to invalid credentials like a wrong API key.
  2. Step 2: Analyze code behavior

    The code sends a wrong API key, so the server should respond with 401 Unauthorized.
  3. Final Answer:

    401 -> Option D
  4. Quick Check:

    Invalid API key response = 401 [OK]
Quick Trick: Invalid API keys usually return 401 Unauthorized status [OK]
Common Mistakes:
  • Confusing 401 with 404 Not Found
  • Expecting 200 OK despite invalid key
  • Thinking server returns 500 error for auth issues

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes