FastAPI default response for specific status
Question:
here we can declare what status code should be sent to client when the endpoint is called:
@router.post("/", status_code=status.HTTP_201_CREATED)
the problem I faced for a response body I must return some something be it a JSONResponse
or PlainTextResponse
and I wonder if it’s possible to not return anything in the router body, but define a default behavior and response for any status code like this for example:
@router.post("/", status_code=status.HTTP_201_CREATED)
async def create_post(req: post_schemas.Post):
# create the post record
# I wanna get rid of this part and do this automatically in a way
return PlainTextResponse(status_code=status.HTTP_201_CREATED, content="Created")
and the client gets "Created" message instead of null
EDIT
This is what I came up with
responses = {200: "OK", 201: "Created"}
@app.middleware("http")
async def no_response_middleware(request: Request, call_next):
response = await call_next(request)
if (
response.status_code in responses
and int(response.headers["content-length"]) == 4
):
return PlainTextResponse(
status_code=response.status_code, content=responses.get(response.status_code)
)
return response
Answers:
It’s doable with middleware
It can get access to the response before it’s returned to the client. Meaning you can write custom logic there and replace it if needed
@app.middleware("http")
async def no_response_middleware(request: Request, call_next):
response = await call_next(request)
if is_null_response(response) and response.status_code == 201: # Maybe just a check for 201
return MyDefaultResponse
return response
here we can declare what status code should be sent to client when the endpoint is called:
@router.post("/", status_code=status.HTTP_201_CREATED)
the problem I faced for a response body I must return some something be it a JSONResponse
or PlainTextResponse
and I wonder if it’s possible to not return anything in the router body, but define a default behavior and response for any status code like this for example:
@router.post("/", status_code=status.HTTP_201_CREATED)
async def create_post(req: post_schemas.Post):
# create the post record
# I wanna get rid of this part and do this automatically in a way
return PlainTextResponse(status_code=status.HTTP_201_CREATED, content="Created")
and the client gets "Created" message instead of null
EDIT
This is what I came up with
responses = {200: "OK", 201: "Created"}
@app.middleware("http")
async def no_response_middleware(request: Request, call_next):
response = await call_next(request)
if (
response.status_code in responses
and int(response.headers["content-length"]) == 4
):
return PlainTextResponse(
status_code=response.status_code, content=responses.get(response.status_code)
)
return response
It’s doable with middleware
It can get access to the response before it’s returned to the client. Meaning you can write custom logic there and replace it if needed
@app.middleware("http")
async def no_response_middleware(request: Request, call_next):
response = await call_next(request)
if is_null_response(response) and response.status_code == 201: # Maybe just a check for 201
return MyDefaultResponse
return response