Bird
0
0

Consider this snippet of a webhook registration handler in Python Flask:

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

Consider this snippet of a webhook registration handler in Python Flask:

from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/register', methods=['POST'])
def register_webhook():
    data = request.json
    if 'callback_url' not in data or 'event' not in data:
        return jsonify({'error': 'Missing fields'}), 400
    # Assume registration logic here
    return jsonify({'status': 'success', 'message': 'Webhook registered'})

What error will occur if the client sends an empty POST body?

ANo error, returns success
BTypeError because request.json is a string
CTypeError because data is None and keys are accessed
D404 Not Found error
Step-by-Step Solution
Solution:
  1. Step 1: Understand request.json behavior on empty body

    If the POST body is empty, request.json returns None, not a dictionary.
  2. Step 2: Analyze the if condition

    The code tries to check keys in data, but data is None, so 'in' operation causes a TypeError.
  3. Final Answer:

    TypeError because data is None and keys are accessed -> Option C
  4. Quick Check:

    Empty body means request.json is None causing key access error [OK]
Quick Trick: Empty JSON body makes request.json None, causing TypeError [OK]
Common Mistakes:
MISTAKES
  • Assuming empty body returns empty dict
  • Expecting success without data
  • Confusing error types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes