0
0
FastAPIframework~10 mins

Health check endpoints 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 create a basic health check endpoint in FastAPI.

FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/health")
async def health_check():
    return {"status": "[1]"}
Drag options to blanks, or click blank then click option'
Aok
Bhealthy
Crunning
Dup
Attempts:
3 left
💡 Hint
Common Mistakes
Using a status message that is not a string
Forgetting to return a dictionary
Using a wrong HTTP method
2fill in blank
medium

Complete the code to add a health check endpoint that returns HTTP status 200 with JSON response.

FastAPI
from fastapi import FastAPI
from fastapi.responses import Response

app = FastAPI()

@app.get("/health")
async def health_check():
    return Response(content='{"status": "ok"}', media_type="[1]")
Drag options to blanks, or click blank then click option'
Atext/html
Btext/plain
Capplication/xml
Dapplication/json
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text/plain' which is for plain text
Using 'application/xml' which is for XML data
Using 'text/html' which is for HTML pages
3fill in blank
hard

Fix the error in the health check endpoint that should return a JSON response with status code 200.

FastAPI
from fastapi import FastAPI
from fastapi.responses import JSONResponse

app = FastAPI()

@app.get("/health")
def health_check():
    return [1]({"status": "ok"}, status_code=200)
Drag options to blanks, or click blank then click option'
AResponse
BJSONResponse
CPlainTextResponse
DHTMLResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using Response which requires manual JSON string
Using PlainTextResponse which returns plain text
Using HTMLResponse which returns HTML
4fill in blank
hard

Fill both blanks to create a health check endpoint that returns a JSON with status 'ok' and HTTP status 200.

FastAPI
from fastapi import FastAPI
from fastapi.responses import [1]

app = FastAPI()

@app.get("/health")
async def health_check():
    return [2]({"status": "ok"}, status_code=200)
Drag options to blanks, or click blank then click option'
AJSONResponse
BResponse
CPlainTextResponse
DHTMLResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Response but returning JSONResponse
Using PlainTextResponse which does not return JSON
Mismatching import and return classes
5fill in blank
hard

Fill all three blanks to create a health check endpoint that returns a JSON with a dynamic status message and HTTP status 200.

FastAPI
from fastapi import FastAPI
from fastapi.responses import [1]

app = FastAPI()

@app.get("/health/{status_msg}")
async def health_check(status_msg: str):
    return [2]({"status": [3], status_code=200)
Drag options to blanks, or click blank then click option'
AJSONResponse
BResponse
Cstatus_msg
D"status_msg"
Attempts:
3 left
💡 Hint
Common Mistakes
Putting quotes around status_msg which makes it a string literal
Using Response instead of JSONResponse
Not importing JSONResponse