Python requires the function body to be indented. Here, print("Fetching data") is not indented properly.
Step 2: Verify decorator and path syntax
The decorator @app.get('/data') is correct, and the path starting with '/' is valid in FastAPI.
Final Answer:
Indentation error in the function body. -> Option A
Quick Check:
Python functions need indented bodies [OK]
Hint: Check indentation inside function after decorator [OK]
Common Mistakes:
Ignoring Python indentation rules
Thinking path cannot start with '/'
Assuming decorator needs no parentheses
5. You want to create a FastAPI route that handles both GET and POST requests on the path '/process'. Which is the correct way to write the decorators?
hard
A. @app.get_post('/process')
B. @app.get('/process')
@app.post('/process')
C. @app.route('/process', methods=['GET', 'POST'])
D. @app.get('/process', methods=['GET', 'POST'])
Solution
Step 1: Understand FastAPI route decorators
FastAPI does not have a combined decorator for multiple methods. Instead, you add multiple decorators for each method on the same function.
Step 2: Check each option's validity
@app.get('/process')
@app.post('/process') uses two decorators @app.get and @app.post on the same function, which is correct. @app.route('/process', methods=['GET', 'POST']) uses Flask-style syntax which is invalid in FastAPI. Options A and D use non-existent decorators or invalid parameters.
Final Answer:
@app.get('/process')
@app.post('/process') -> Option B
Quick Check:
Multiple decorators for multiple methods [OK]
Hint: Use multiple decorators for multiple HTTP methods [OK]
Common Mistakes:
Trying to use Flask-style @app.route
Assuming combined decorator exists
Passing methods param to @app.get
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.