Bird
0
0

Identify the bug in this webhook registration endpoint code snippet:

medium📝 Debug Q6 of 15
Rest API - Webhooks and Events

Identify the bug in this webhook registration endpoint code snippet:
def register_webhook(request):
  callback = request.json['callback_url']
  if callback is None:
    return {'error': 'Missing callback_url'}, 400
  return {'message': 'Webhook registered'}, 201

AThe status code 201 is incorrect for successful registration
BThe callback variable should be named 'url' instead
CThe function should return 500 on missing callback_url
DAccessing 'callback_url' without checking if key exists causes KeyError
Step-by-Step Solution
Solution:
  1. Step 1: Check how 'callback_url' is accessed

    Using request.json['callback_url'] directly raises KeyError if key missing.
  2. Step 2: Understand proper safe access

    Should use .get('callback_url') to avoid exceptions and handle missing keys gracefully.
  3. Final Answer:

    Accessing 'callback_url' without checking if key exists causes KeyError -> Option D
  4. Quick Check:

    Direct key access risks KeyError [OK]
Quick Trick: Use .get() to safely access JSON keys [OK]
Common Mistakes:
MISTAKES
  • Ignoring KeyError risk
  • Misusing status codes
  • Changing variable names unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes