Python Flask Intentional Empty Response

PythonFlaskResponse

Python Problem Overview


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 output

A 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

Python Solutions


Solution 1 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRukTechView Question on Stackoverflow
Solution 1 - PythonMartijn PietersView Answer on Stackoverflow