Bird
0
0

What is the issue with this Flask polling endpoint code?

medium📝 Debug Q6 of 15
Flask - WebSocket and Real-Time
What is the issue with this Flask polling endpoint code?
@app.route('/poll')
def poll():
    data = {'count': 10}
    return json.dumps(data)
Ajson.dumps is not imported, causing a NameError
Bjson.dumps returns a string, but Flask expects a Response object or tuple
CThe route decorator is missing the methods argument
DReturning a dictionary directly is preferred over json.dumps
Step-by-Step Solution
Solution:
  1. Step 1: Understand return types in Flask

    Flask expects a Response, string, or tuple; json.dumps returns a JSON string but without proper content-type.
  2. Step 2: Identify missing content-type

    Returning json.dumps(data) returns a string but Flask won't set 'application/json' automatically.
  3. Final Answer:

    json.dumps returns a string, but Flask expects a Response object or tuple -> Option B
  4. Quick Check:

    Use jsonify to set content-type automatically [OK]
Quick Trick: jsonify sets content-type; json.dumps does not [OK]
Common Mistakes:
MISTAKES
  • Forgetting to import json module
  • Assuming json.dumps sets response headers
  • Not using jsonify for JSON responses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes