Complete the code to import the base class for custom responses in FastAPI.
from fastapi.responses import [1]
The Response class is the base class for creating custom response classes in FastAPI.
Complete the code to create a custom response class named MyResponse inheriting from the base response class.
class MyResponse([1]): def __init__(self, content: str): super().__init__(content=content, media_type="text/custom")
Custom response classes should inherit from Response to define their own behavior.
Fix the error in the custom response class by completing the method that returns the response body as bytes.
class MyResponse(Response): def __init__(self, content: str): super().__init__(content=content, media_type="text/custom") def [1](self) -> bytes: return self.content.encode("utf-8")
The render method is used in FastAPI custom responses to convert content to bytes.
Fill both blanks to create a FastAPI route that returns the custom response with content 'Hello'.
from fastapi import FastAPI app = [2]() @app.get("/custom") async def custom_route(): return [1]("Hello") # Use custom response class
The route should return an instance of the custom response class MyResponse. The app is created by calling FastAPI().
Fill all three blanks to define a custom response class that sets a custom header and returns content as bytes.
from fastapi.responses import Response class CustomHeaderResponse([1]): def __init__(self, content: str): headers = {"X-Custom-Header": [2] super().__init__(content=content, media_type="text/custom", headers=headers) def [3](self) -> bytes: return self.content.encode("utf-8")
The custom response class inherits from Response, sets a header string value, and implements the render method to return bytes.