When the browser makes a poll to get the latest messages, our server should return the messages in a JSON format. To achieve this, we'll need to create a new HTTP endpoint that returns the messages as JSON, without using the Jinja2 templating. We will first construct a new helper function to create a JSON response, setting the correct headers.
Outside of our WebServer, create the following function:
def create_json_response(content): headers = {'Content-Type': 'application/json'} json_data = json.dumps(content) return Response(json_data, status=200, headers=headers)
This is similar to our create_html_response from earlier, but here it sets the Content-Type to 'application/json' and converts our data into a valid ...