Fastapi: [QUESTION] How to response with empty body?

Created on 26 Feb 2020  路  8Comments  路  Source: tiangolo/fastapi

Though I neither declare response model (None by default) neither return anything the response contains null. Is there a way to send empty body?

question

Most helpful comment

This works as I need:
return Response(status_code=HTTP_200_OK)

Thanks all!

All 8 comments

First check

  • [ ] I used the GitHub search to find a similar issue and didn't find it.
  • [ ] I searched the FastAPI documentation, with the integrated search.
  • [ ] I already searched in Google "How to X in FastAPI" and didn't find any information.

https://github.com/tiangolo/fastapi/issues/449#issuecomment-523371704

Do you want to return HTTP_204_NO_CONTENT? If it is true, remove return statement from your endpoint handler.

I know in Flask you had to do something like return '', 200. You could try returning an empty string. There's probably a cleaner way, though.

@alvarogf97

Do you want to return HTTP_204_NO_CONTENT? If it is true, remove return statement from your endpoint handler.

I want to return 200 and no body at all.
If I remove return response status is 200 and body is "null".

This works as I need:
return Response(status_code=HTTP_200_OK)

Thanks all!

Thanks for the help here everyone! :cake:

Thanks @symonsoft for reporting back and closing the issue :+1:

imports for the answer from @symonsoft are

from starlette.responses import Response
from starlette.status import HTTP_200_OK

Was this page helpful?
0 / 5 - 0 ratings