Bird
0
0

Find the bug in this Flask route that should return 404 if a book is not found:

medium📝 Debug Q7 of 15
Rest API - HTTP Status Codes
Find the bug in this Flask route that should return 404 if a book is not found:
@app.route('/books/')
def get_book(id):
    books = {1: '1984', 2: 'Brave New World'}
    if id not in books:
        return 'Book not found', 404
    return books[id], 200
AThe route should use abort(404) instead of returning manually
BReturning a string with status code is correct; no bug here
CThe return statement for found book should not include status code 200
DThe books dictionary keys should be strings, not integers
Step-by-Step Solution
Solution:
  1. Step 1: Check how Flask handles return with status code

    Flask allows returning a tuple (body, status_code) which is valid.
  2. Step 2: Verify the logic for 404 and 200 responses

    If id not found, returns 'Book not found' with 404; else returns book with 200.
  3. Final Answer:

    Returning a string with status code is correct; no bug here -> Option B
  4. Quick Check:

    Flask supports return (body, status) tuples [OK]
Quick Trick: Flask can return (body, status) tuples directly [OK]
Common Mistakes:
  • Thinking abort(404) is mandatory
  • Assuming status code 200 must be omitted
  • Confusing key types in dictionary

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes