Skip to content

Instantly share code, notes, and snippets.

@daaniam
Created June 8, 2023 05:34
Show Gist options
  • Save daaniam/00eafdba7203c5cda5e2bc29b2970176 to your computer and use it in GitHub Desktop.
Save daaniam/00eafdba7203c5cda5e2bc29b2970176 to your computer and use it in GitHub Desktop.
FastAPI custom response with data & errors
class MyCUstomResponse(JSONResponse):
def __init__(
self,
data: list[t.Any],
errors: list[t.Any] | None = None,
status_code: int = 200,
headers: dict[str, str] | None = None,
media_type: str | None = None,
background: BackgroundTask | None = None,
) -> None:
self.data = data
if errors is None:
errors = []
self.errors = errors
self.content = {"data": self.data, "errors": self.errors}
super().__init__(
content=self.content,
status_code=status_code,
headers=headers,
media_type=media_type,
background=background,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment