Consider this FastAPI endpoint using a custom response class:
from fastapi import FastAPI, Response
app = FastAPI()
class CustomTextResponse(Response):
media_type = "text/custom"
@app.get("/custom", response_class=CustomTextResponse)
async def custom_response():
return "Hello Custom!"
What will the client receive as the Content-Type header and body?
from fastapi import FastAPI, Response app = FastAPI() class CustomTextResponse(Response): media_type = "text/custom" @app.get("/custom", response_class=CustomTextResponse) async def custom_response(): return "Hello Custom!"
Check the media_type attribute in the custom response class.
The custom response class sets media_type to text/custom, so the response header Content-Type will be text/custom. The returned string is the body.
You want to create a custom JSON response class that sets media_type to application/vnd.api+json. Which code snippet is correct?
FastAPI's JSONResponse already handles JSON encoding.
Option B correctly subclasses JSONResponse and overrides media_type. This reuses JSON encoding logic. Other options either miss encoding to bytes or use wrong return types.
Examine this code snippet:
from fastapi import FastAPI, Response
app = FastAPI()
class MyResponse(Response):
media_type = "text/plain"
def render(self, content):
return content
@app.get("/test", response_class=MyResponse)
async def test():
return "Hello"
What error will occur and why?
from fastapi import FastAPI, Response app = FastAPI() class MyResponse(Response): media_type = "text/plain" def render(self, content): return content @app.get("/test", response_class=MyResponse) async def test(): return "Hello"
Check the return type expected by render in FastAPI's Response classes.
The render method must return bytes, but here it returns a string. This causes a TypeError at runtime.
Given this FastAPI app:
from fastapi import FastAPI, Response
app = FastAPI()
class XMLResponse(Response):
media_type = "application/xml"
def render(self, content: dict) -> bytes:
xml = ""
for key, value in content.items():
xml += f"<{key}>{value}{key}>"
xml += " "
return xml.encode("utf-8")
@app.get("/xml", response_class=XMLResponse)
async def get_xml():
return {"name": "Alice", "age": 30}
What will the client receive as the response body?
from fastapi import FastAPI, Response app = FastAPI() class XMLResponse(Response): media_type = "application/xml" def render(self, content: dict) -> bytes: xml = "<root>" for key, value in content.items(): xml += f"<{key}>{value}</{key}>" xml += "</root>" return xml.encode("utf-8") @app.get("/xml", response_class=XMLResponse) async def get_xml(): return {"name": "Alice", "age": 30}
Look at how the render method builds the XML string.
The render method converts the dictionary to XML tags without quotes around values. So the output is <root><name>Alice</name><age>30</age></root>.
Choose the correct statement about creating and using custom response classes in FastAPI.
Think about what FastAPI expects from response classes regarding data encoding.
FastAPI requires custom response classes to subclass Response and implement render that returns bytes. This ensures proper HTTP response formatting.