Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import HTTPException from FastAPI.
FastAPI
from fastapi import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated classes like Request or Response.
Forgetting to import HTTPException before using it.
✗ Incorrect
You need to import HTTPException from fastapi to raise HTTP errors.
2fill in blank
mediumComplete the code to raise a 404 HTTPException with a detail message.
FastAPI
raise HTTPException(status_code=[1], detail="Item not found")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 which means success.
Using 500 which means server error.
✗ Incorrect
404 is the standard HTTP status code for 'Not Found'.
3fill in blank
hardFix the error in raising HTTPException with a missing status_code argument.
FastAPI
raise HTTPException([1]=401, detail="User not authorized")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
detail instead of status_code as the first argument.Omitting the status_code argument entirely.
✗ Incorrect
The status_code argument is required to specify the HTTP error code.
4fill in blank
hardFill both blanks to raise a 403 HTTPException with a custom detail message.
FastAPI
raise HTTPException(status_code=[1], detail=[2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 401 which means unauthorized, not forbidden.
Not quoting the detail message string.
✗ Incorrect
403 means forbidden access. The detail message explains the reason.
5fill in blank
hardFill both blanks to raise a 422 HTTPException with a JSON detail explaining the error.
FastAPI
raise HTTPException(status_code=[1], detail=[2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 400 which is a bad request but less specific.
Passing detail as a plain string instead of JSON string.
✗ Incorrect
422 means unprocessable entity. The detail can be a JSON string explaining the error.