Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tornado - CORS PUT

I am facing a problem with Tornado. I have an API endpoint for PUT HTTP Method in Tornado. I also have a web application that sends the request to this API with jQuery and AJAX, but always I get a 405 response because the request is going as HTTP Method OPTIONS. I understand the way it works and I did configured my Tornado Server to allow it. But even so I having this situation. Can someone help me?

There is my server code:

class BaseHandler(RequestHandler):
   def __init__(self, *args, **kwargs):
        super(BaseHandler, self).__init__(*args, **kwargs)
        self.set_header('Cache-Control', 'no-store, no-cache, must-   revalidate, max-age=0')
        self.set_header("Access-Control-Allow-Origin", "*")
        self.set_header("Access-Control-Allow-Headers", "Content-Type")
        self.set_header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS')

Many thanks

like image 763
Ivens Leão Avatar asked Apr 01 '26 07:04

Ivens Leão


1 Answers

You need to add an options handler that just sends the headers with no body:

def options(self):
    # no body
    self.set_status(204)
    self.finish()

See Tornado server: enable CORS requests for a complete code snippet.

Or else just install the tornado-cors package:

pip install tornado-cors

That will add the necessary handlers for you, and ensure the right response headers get sent.

like image 67
2 revs, 2 users 97%sideshowbarker Avatar answered Apr 03 '26 20:04

2 revs, 2 users 97%sideshowbarker