Complete the code to import FastAPI and create an app instance.
from fastapi import [1] app = [1]()
The FastAPI class is imported and used to create the app instance.
Complete the code to define a GET endpoint that returns JSON.
@app.get("/items/{item_id}") async def read_item(item_id: int): return [1]
The endpoint returns a dictionary which FastAPI converts to JSON.
Fix the error in the response type by completing the code to return a plain text response.
from fastapi.responses import [1] @app.get("/plaintext") async def get_plaintext(): return [1]("Hello World")
Use PlainTextResponse to return plain text content.
Fill both blanks to return an HTML response with a custom status code.
from fastapi.responses import [1] @app.get("/custom_html") async def custom_html(): content = "<h1>Welcome</h1>" return [1](content=content, status_code=[2])
Use HTMLResponse to return HTML content and set status_code=404 for custom status.
Fill all three blanks to return a JSON response with a custom header and status code.
from fastapi.responses import [1] @app.get("/custom_json") async def custom_json(): data = {"message": "Success"} headers = [2] return [1](content=data, status_code=[3], headers=headers)
Use JSONResponse to return JSON data, add custom headers as a dict, and set status code 201 for Created.
