Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tornado RESTful PUT handler method not getting request arguments

I'm trying to unit test my RESTful API. Here's my API:


class BaseHandler(tornado.web.RequestHandler):                    
    def __init__(self, *args, **kwargs):                          
        tornado.web.RequestHandler.__init__(self, *args, **kwargs)
        self.log = self.application.log                           
        self.db = self.application.db                             

class ProductHandler(BaseHandler):
    @tornado.web.removeslash
    def put(self, id = None, *args, **kwargs):
        try:
            self.log.info("Handling PUT request")                                                             
            if not id:                                                                                                      
                raise Exception('Object Id Required')                                                                        
            id = { '_id' : id }                                                                                                                            
            new_values = dict()                                                                                             
            name = self.get_argument('name', None)                                                                          
            description = self.get_argument('description', None)                                                            
            if name:                                                                                                        
                new_values['name'] = name                                                                                   
            if description:                                                                                                 
                new_values['description'] = description                                                                     
            self.db.products.update(id, new_values, safe = True)                                                                                                               
        except:
            self.log.error("".join(tb.format_exception(*sys.exc_info())))                                                   
            raise                                                                                                           

 class Application(tornado.web.Application):                         
     def __init__(self, config_path, test = False, *args, **kwargs): 
         handlers = [                                                
             (r"/product/?(.*)", ProductHandler),                    
         ]                                                           
         settings = dict(debug=True)                                 
         tornado.web.Application.__init__(self, handlers, **settings)
         self.log = logging.getLogger(__name__)                      
         self.config = ConfigParser()                                
         self.config.read(config_path)                               
         self.mongo_connection = Connection(                         
             host = self.config.get('mongo','host'),                 
             port = self.config.getint('mongo','port'),              
         )                                                           
         if test:                                                    
             db_name = self.config.get('test', 'mongo.db')           
         else:                                                       
             db_name = self.config.get('mongo', 'db')                
         self.log.debug("Using db:  %s" % db_name)                   
         self.db = self.mongo_connection[db_name]                    

But, here's my problem: the handler isn't seeing the name or description arguments. :(

Any suggestions?

like image 297
bitcycle Avatar asked Aug 14 '12 22:08

bitcycle


3 Answers

As a work-around, I found them in the request.body and parsed the encoded parameters manually. It was kindof annoying, but it works.


new_values = urlparse.parse_qs(self.request.body)

# values show as lists with only one item
for k in new_values:                             
    new_values[k] = new_values[k][0]             
like image 56
bitcycle Avatar answered Oct 31 '22 22:10

bitcycle


Say if you are using jQuery to send this PUT request:

$.ajax({
    type: "PUT",
    url: "/yourURL",
    data: JSON.stringify({'json':'your json here'),
    dataType: 'json'
})

The data should not be like: data: {'json': 'your json here'}, because it will automatically be encoded into query string, which needs to be parsed by parse_qs

Then in Tornado

def put(self, pid):
    d = json.loads(self.request.body)
    print d
like image 20
MK Yung Avatar answered Oct 31 '22 22:10

MK Yung


put handler will parse request.body, if request had proper content-type header (application/x-www-form-urlencoded), for example if you are using tornado http client:

headers = HTTPHeaders({'content-type': 'application/x-www-form-urlencoded'})
http_client.fetch(
      HTTPRequest(url, 'PUT', body=urllib.urlencode(body), headers=headers))
like image 25
Konstantin Lopuhin Avatar answered Oct 31 '22 23:10

Konstantin Lopuhin