Python Flask Intentional Empty Response
69,539
You are responding to a request, your HTTP server must return something. The HTTP 'empty response' response is 204 No Content
:
return ('', 204)
Note that returning a file to the browser is not an empty response, just different from a HTML response.
Related videos on Youtube
Author by
RukTech
Updated on December 20, 2020Comments
-
RukTech over 3 years
Is there a way to return a response (from
make_response()
object or similar) with certain properties so that it doesn't render the page again and doesn't do anything else either. I am trying to run a code on the server without generating any outputA simple 'return None' produces:
ValueError: View function did not return a response
This should be possible because the following only downloads a file and doesn't render the template:
myString = "First line of a document" response = make_response(myString) response.headers["Content-Disposition"] = "attachment; filename=myFile.txt" return response
-
dtk over 8 yearsFtr: you may use
httplib.NO_CONTENT
to avoid the magic number. -
bfontaine almost 7 yearsNote the equivalent of Python 2’s
httplib.NO_CONTENT
in Python 3 ishttp.HTTPStatus.NO_CONTENT
. -
Martijn Pieters almost 7 years@bfontaine: or
http.client.NO_CONTENT
-
Martijn Pieters almost 7 years@bfontaine: at the bottom of the
http
package docs; these used to be the normal location until theHTTPStatus
enum was introduced, see github.com/python/cpython/commit/…