0
0
FastAPIframework~10 mins

HTTPException usage in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import HTTPException from FastAPI.

FastAPI
from fastapi import [1]
Drag options to blanks, or click blank then click option'
AHTTPException
BRequest
CResponse
DDepends
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated classes like Request or Response.
Forgetting to import HTTPException before using it.
2fill in blank
medium

Complete 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'
A302
B404
C500
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 which means success.
Using 500 which means server error.
3fill in blank
hard

Fix 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'
Adetail
Bmessage
Cstatus_code
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using detail instead of status_code as the first argument.
Omitting the status_code argument entirely.
4fill in blank
hard

Fill 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'
A403
B"Access denied"
C"Not allowed"
D401
Attempts:
3 left
💡 Hint
Common Mistakes
Using 401 which means unauthorized, not forbidden.
Not quoting the detail message string.
5fill in blank
hard

Fill 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'
A422
B{"error": "Invalid input"}
C"Invalid input"
D400
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.