0
0
FastAPIframework~20 mins

Multiple response types in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Multiple Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when requesting JSON from this FastAPI endpoint?

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?

FastAPI
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"}
AA plain text response with body {"message": "Hello"} and content-type text/plain
BAn XML response with body <data>Hello</data> and content-type application/xml
CA 404 Not Found error because the accept header is not handled
DA JSON response with body {"message": "Hello"} and content-type application/json
Attempts:
2 left
💡 Hint

Check the default value of the accept parameter and what the function returns in that case.

📝 Syntax
intermediate
2:00remaining
Which option correctly sets multiple response types in FastAPI?

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?

A
from fastapi import FastAPI, Response
app = FastAPI()

@app.get("/item")
async def item(text: bool = False):
    if text:
        return Response(content="Hello", media_type="text/plain")
    return {"message": "Hello"}
B
from fastapi import FastAPI
app = FastAPI()

@app.get("/item")
async def item(text: bool = False):
    if text:
        return {"message": "Hello"}
    return Response(content="Hello", media_type="text/plain")
C
from fastapi import FastAPI, Response
app = FastAPI()

@app.get("/item")
async def item(text: bool = False):
    if text:
        return Response(content="Hello")
    return {"message": "Hello"}
D
from fastapi import FastAPI
app = FastAPI()

@app.get("/item")
async def item(text: bool = False):
    return Response(content="Hello", media_type="text/plain")
Attempts:
2 left
💡 Hint

Remember that to return plain text, you must specify the media_type in Response.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI endpoint always return JSON even when requesting XML?

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?

FastAPI
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"}
ABecause the accept parameter is not read from headers but from query parameters
BBecause FastAPI does not support XML responses
CBecause returning a string always produces JSON in FastAPI
DBecause the response media type is not set to application/xml, FastAPI defaults to JSON
Attempts:
2 left
💡 Hint

Check how the accept parameter is passed and used.

state_output
advanced
2:00remaining
What is the content-type of the response for this FastAPI endpoint?

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?

FastAPI
from fastapi import FastAPI, Response
app = FastAPI()

@app.get("/custom")
async def custom(response: Response):
    response.headers["Content-Type"] = "application/custom"
    return {"data": "value"}
Atext/plain
Bapplication/custom
Capplication/json
DNo Content-Type header
Attempts:
2 left
💡 Hint

Think about how FastAPI sets content-type when returning a dict.

🧠 Conceptual
expert
3:00remaining
How to correctly handle multiple response types with FastAPI's Response class?

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?

AReturn JSON always and set response.headers['Content-Type'] to 'application/xml' when XML is requested.
BReturn a Response object with content as XML string and media_type='application/xml' when XML is requested; otherwise return a dict for JSON.
CReturn a string with XML content and rely on FastAPI to detect media type automatically.
DReturn a dict with XML string inside when XML is requested and set response.media_type to 'application/xml'.
Attempts:
2 left
💡 Hint

Consider how FastAPI determines response content type and how to override it properly.