Skip to content

Instantly share code, notes, and snippets.

@bengrunfeld
Created January 13, 2015 21:09
Show Gist options
  • Save bengrunfeld/cd11750d16caacea79e1 to your computer and use it in GitHub Desktop.
Save bengrunfeld/cd11750d16caacea79e1 to your computer and use it in GitHub Desktop.
Webapp2 Headers
"""
Sometimes you need to forcefully set headers in Webapp2 while using Google App Engine (GAE).
It is NOT RECOMMENDED to use `*` for `Access-Control-Allow-Origin` as below. I've put it
there simply for the sake of demonstration. Make sure you choose something more restrictive.
"""
def initialize_headers(headers, http_verb):
"""Set up the headers for HTTP requests"""
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = http_verb
headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept'
headers['Content-Type'] = 'text/plain'
return headers
class HandleOptions(webapp2.RequestHandler):
def options(self):
"""GET /: Retrieve all todos"""
self.response.headers = initialize_headers(self.response.headers, 'OPTIONS')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment