I'm setting headers for my requests this way:
class ContactInfoHandler(tornado.web.RequestHandler):
def set_default_headers(self):
print "setting headers!!!"
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Headers", "x-requested-with")
self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
def get(self, *args, **kwargs):
self.write('something')
I have to do it for all of my handlers, Is there a way to do it one in whole my Tornado project?
You can write a handler that is inherited from tornado.web.RequestHandler
, then all handlers used as an API can be inherited from this handler. Here is the example below.
from tornado.web import RequestHandler
class BaseHandler(RequestHandler):
def get(self, *args, **kwargs):
self.write("say something")
def set_default_headers(self, *args, **kwargs):
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Headers", "x-requested-with")
self.set_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
As you have done this step, you can totally inherit what BaseHandler
can do by writing handlers inherited from BaseHandler
.
class ContactInfoHandler(BaseHandler):
def get(self, *args, **kwargs):
self.write("something")
class TestInfoHandler(BaseHandler):
def post(self, *args, **kwargs):
self.write("test result is here")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With