Consider this FastAPI endpoint that returns different response types based on the accept header.
from fastapi import FastAPI, Response
app = FastAPI()
@app.get("/data")
async def get_data(response: Response, accept: str = "application/json"):
if accept == "application/xml":
response.media_type = "application/xml"
return "<data>Hello</data>"
return {"message": "Hello"}What will the client receive if it sends a request with accept header set to application/json?
from fastapi import FastAPI, Response app = FastAPI() @app.get("/data") async def get_data(response: Response, accept: str = "application/json"): if accept == "application/xml": response.media_type = "application/xml" return "<data>Hello</data>" return {"message": "Hello"}
Check the default value of the accept parameter and what the function returns in that case.
The endpoint returns a JSON dictionary when the accept header is application/json (default). The response media type is not changed, so FastAPI uses the default application/json. Thus, the client receives JSON with the message.
You want to create a FastAPI endpoint that can return either JSON or plain text based on a query parameter. Which code snippet correctly implements this?
Remember that to return plain text, you must specify the media_type in Response.
Option A correctly returns a JSON dictionary by default and a plain text Response when text=True. Option A reverses the logic. Option A misses the media_type for plain text, so no Content-Type header is set, which is incorrect. Option A always returns plain text, ignoring the query parameter.
Look at this FastAPI endpoint:
from fastapi import FastAPI
app = FastAPI()
@app.get("/info")
async def info(accept: str):
if accept == "application/xml":
return "<info>Data</info>"
return {"info": "Data"}When a client sends the header accept: application/xml, the response is still JSON. Why?
from fastapi import FastAPI app = FastAPI() @app.get("/info") async def info(accept: str): if accept == "application/xml": return "<info>Data</info>" return {"info": "Data"}
Check how the accept parameter is passed and used.
The accept parameter is a function argument, so FastAPI expects it as a query parameter, not from the HTTP headers. The client sending an HTTP header accept does not set the query parameter. Thus, the condition is never true, and JSON is always returned.
Given this FastAPI endpoint:
from fastapi import FastAPI, Response
app = FastAPI()
@app.get("/custom")
async def custom(response: Response):
response.headers["Content-Type"] = "application/custom"
return {"data": "value"}What will be the content-type header in the HTTP response?
from fastapi import FastAPI, Response app = FastAPI() @app.get("/custom") async def custom(response: Response): response.headers["Content-Type"] = "application/custom" return {"data": "value"}
Think about how FastAPI sets content-type when returning a dict.
FastAPI sets the content-type header automatically based on the return type. Setting response.headers["Content-Type"] directly is ignored or overwritten when returning a dict. The content-type will be application/json.
You want to create a FastAPI endpoint that returns JSON by default but can return XML when requested. Which approach correctly handles setting the response content and media type?
Consider how FastAPI determines response content type and how to override it properly.
Option B correctly uses the Response class to set both content and media_type explicitly for XML. Returning a dict always results in JSON. Setting headers directly does not change the response body or media type properly. FastAPI does not auto-detect XML from string return values.