Complete the code to create a basic health check endpoint in FastAPI.
from fastapi import FastAPI app = FastAPI() @app.get("/health") async def health_check(): return {"status": "[1]"}
The health check endpoint returns a simple JSON with status 'ok' to indicate the service is running.
Complete the code to add a health check endpoint that returns HTTP status 200 with JSON response.
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]")
The media type for JSON responses is 'application/json'. This tells the client to expect JSON data.
Fix the error in the health check endpoint that should return a JSON response with status code 200.
from fastapi import FastAPI from fastapi.responses import JSONResponse app = FastAPI() @app.get("/health") def health_check(): return [1]({"status": "ok"}, status_code=200)
To return JSON with a specific status code, use FastAPI's JSONResponse class.
Fill both blanks to create a health check endpoint that returns a JSON with status 'ok' and HTTP status 200.
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)
Import JSONResponse and use it to return JSON data with status code 200.
Fill all three blanks to create a health check endpoint that returns a JSON with a dynamic status message and HTTP status 200.
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)
Import and use JSONResponse to return JSON. Use the variable status_msg without quotes to insert its value dynamically.