2017-05-04 140 views
-2

以下是我的处理程序代码,其中龙卷风允许执行获取请求,因为获取方法不允许出错。 我缺少一些明显的东西?龙卷风不允许放入请求

class CustomerHandler(web.RequestHandler): 
      def get(self, customer_id): 
      data = retrieve_customer_data_from_customer_database(customer_id) 
      print(data) 
      self.write(data) 
      self.finish() 

      def put(self, data): 
       customer_data = data 
       data = json.loads(customer_data) 
       customer_id = customer_data['id'] 
       update_customer_data(customer_id, data) 
       result_out = {} 
       result_out['status'] = True 
       self.write(json.dumps(result_out)) 
       self.finish() 
+1

你的缩进是非常错误的;请仔细检查一下,因为这实际上可能是唯一的问题。 – deceze

+0

对不起,它是格式问题在堆栈溢出端我的代码注册是正确的 – sagar

回答

1

再次检查缩进。此外,您正在寻找的data可能在请求的正文中。这里有一个简单的例子:

import tornado.ioloop 
import tornado.web 
import json 

class MainHandler(tornado.web.RequestHandler): 

    def get(self): 
     self.write("Hello, world") 

    def put(self): 
     body = json.loads(self.request.body) 
     # do some stuff here 
     self.write("{} your ID is {}".format(body['name'], body['id'])) 

if __name__ == "__main__": 
    application = tornado.web.Application([ 
     (r"/", MainHandler), 
    ]) 
    application.listen(8888) 
    tornado.ioloop.IOLoop.current().start() 

而且测试:

$ curl http://localhost:8888/ -XPUT -d '{"id": 123, "name": "John"}' 
John your ID is 123 
0

的问题是有额外的“/”我用PUT请求的URL,同时从前端调用,这就是为什么不允许的方法错误在那里。尽管错误消息不能提示究竟是什么错误。

希望这会帮助别人。