Bird
0
0

What is wrong with this FastAPI code that tries to save data?

medium📝 Debug Q14 of 15
FastAPI - Database Integration
What is wrong with this FastAPI code that tries to save data?
from fastapi import FastAPI

app = FastAPI()

@app.post('/users/')
def create_user(user: dict):
    user['id'] = 1
    return user
AIt uses the wrong HTTP method
BIt does not save data to a database, so data is lost on restart
CIt has a syntax error in the function
DIt commits data twice
Step-by-Step Solution
Solution:
  1. Step 1: Check if data is saved to a database

    The code only modifies and returns a dictionary; it does not save to any database.
  2. Step 2: Understand consequence of no database saving

    Without saving to a database, data is lost when the app stops or restarts.
  3. Final Answer:

    It does not save data to a database, so data is lost on restart -> Option B
  4. Quick Check:

    No database save means no persistence [OK]
Quick Trick: Data must be saved to database for persistence [OK]
Common Mistakes:
MISTAKES
  • Thinking returning dict saves data
  • Confusing HTTP method with persistence
  • Assuming syntax error when none exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes