0
0
FastAPIframework~10 mins

Status code control 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 return a 201 status code from the FastAPI endpoint.

FastAPI
from fastapi import FastAPI, status

app = FastAPI()

@app.post("/items", status_code=[1])
def create_item():
    return {"message": "Item created"}
Drag options to blanks, or click blank then click option'
Astatus.HTTP_201_CREATED
Bstatus.HTTP_200_OK
Cstatus.HTTP_400_BAD_REQUEST
Dstatus.HTTP_404_NOT_FOUND
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 OK instead of 201 Created
Using a client error status code like 400
Not importing the status module
2fill in blank
medium

Complete the code to raise a 404 error when an item is not found.

FastAPI
from fastapi import FastAPI, HTTPException

app = FastAPI()

@app.get("/items/{item_id}")
def read_item(item_id: int):
    if item_id != 1:
        raise HTTPException(status_code=[1], detail="Item not found")
    return {"item_id": item_id}
Drag options to blanks, or click blank then click option'
A404
B200
C500
D403
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 OK instead of 404
Using 500 Internal Server Error incorrectly
Not raising HTTPException
3fill in blank
hard

Fix the error in the code to correctly return a 204 No Content response.

FastAPI
from fastapi import FastAPI, Response, status

app = FastAPI()

@app.delete("/items/{item_id}")
def delete_item(item_id: int):
    # Delete logic here
    return Response(status_code=[1])
Drag options to blanks, or click blank then click option'
A200
B204
C201
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 200 OK with content instead of 204
Using 201 Created for deletion
Not using Response to set status code
4fill in blank
hard

Fill both blanks to return a JSON response with a 202 Accepted status code.

FastAPI
from fastapi import FastAPI, JSONResponse, status

app = FastAPI()

@app.post("/process")
def process_data():
    return JSONResponse(content=[1], status_code=[2])
Drag options to blanks, or click blank then click option'
A{"message": "Processing started"}
Bstatus.HTTP_202_ACCEPTED
C{"error": "Invalid data"}
Dstatus.HTTP_400_BAD_REQUEST
Attempts:
3 left
💡 Hint
Common Mistakes
Using error message content with 202 status
Using 400 Bad Request status code incorrectly
Not returning JSONResponse
5fill in blank
hard

Fill all three blanks to return a custom status code and message in a FastAPI endpoint.

FastAPI
from fastapi import FastAPI, Response

app = FastAPI()

@app.put("/update/{item_id}")
def update_item(item_id: int):
    # Update logic here
    return Response(content=[1], media_type=[2], status_code=[3])
Drag options to blanks, or click blank then click option'
A"Item updated successfully"
B"text/plain"
C202
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using JSON content without setting media_type
Using wrong status code like 404
Not setting media_type for plain text