2017-05-04 141 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,同時從前端調用,這就是爲什麼不允許的方法錯誤在那裏。儘管錯誤消息不能提示究竟是什麼錯誤。

希望這會幫助別人。