Bird
0
0

Find the error in this webhook registration endpoint snippet:

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

Find the error in this webhook registration endpoint snippet:
@app.route('/register', methods=['POST'])
def register():
  data = request.get_json()
  if 'callback_url' not in data:
    return {'error': 'callback_url required'}, 400
  callback = data['callback_url']
  if not callback.startswith('http'):
    return {'error': 'Invalid URL'}, 400
  return {'message': 'Registered'}, 201

Arequest.get_json() can return None if JSON is invalid, causing TypeError
BChecking 'callback_url' key is unnecessary
CThe URL validation should check for 'https' only
DThe route should accept GET instead of POST
Step-by-Step Solution
Solution:
  1. Step 1: Understand request.get_json() behavior

    It returns None if the request body is not valid JSON.
  2. Step 2: Identify risk of NoneType error

    Checking 'callback_url' in None causes TypeError since None is not subscriptable.
  3. Final Answer:

    request.get_json() can return None if JSON is invalid, causing TypeError -> Option A
  4. Quick Check:

    Always check if get_json() returned None [OK]
Quick Trick: Validate get_json() result before accessing keys [OK]
Common Mistakes:
MISTAKES
  • Assuming get_json() never returns None
  • Skipping key existence check
  • Restricting URL scheme too strictly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes