Bird
0
0

Find the bug in this REST API code that should return 409 Conflict on duplicate resource creation: def create_user(user): if user_exists(user['email']): return {"error": "User already exists"}, 400 else: save_user(user) return {"message": "User created"}, 201

medium📝 Debug Q7 of 15
Rest API - HTTP Status Codes
Find the bug in this REST API code that should return 409 Conflict on duplicate resource creation: def create_user(user): if user_exists(user['email']): return {"error": "User already exists"}, 400 else: save_user(user) return {"message": "User created"}, 201
AThe function should return 404 if user exists.
BThe save_user function is called incorrectly.
CThe error message should be "Conflict error".
DIt should return 409 Conflict, not 400 Bad Request, on duplicate user.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the correct status for duplicate resource

    When a resource already exists, the API should return 409 Conflict, not 400 Bad Request.
  2. Step 2: Identify the incorrect status code in the code

    The code returns 400, which is incorrect for this conflict scenario.
  3. Final Answer:

    It should return 409 Conflict, not 400 Bad Request, on duplicate user. -> Option D
  4. Quick Check:

    Duplicate resource creation = 409 Conflict [OK]
Quick Trick: Use 409 for duplicates, not 400 Bad Request [OK]
Common Mistakes:
  • Returning 400 instead of 409
  • Wrong error message text
  • Returning 404 for existing resource

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes