3

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

Grisha Levit
  • 8,194
  • 2
  • 38
  • 53

2 Answers2

2

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.

Community
  • 1
  • 1
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
1

if you don't define put method return 405

class Handler(tornado.web.RequestHandler):
    def put(self):
        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')

[I 170205 04:56:35 web:1971] 200 PUT

Manu Gupta
  • 351
  • 1
  • 5
  • 18
  • Thank you for your reply. This snippet is for my parent class which one I inherit in other handlers. So I have the implementation of the PUT Method in my specific handler :) – Ivens Leão Feb 05 '17 at 02:19