RESTful PATCH request not working

99 views
Skip to first unread message

Falko Delarue

unread,
Aug 12, 2014, 11:50:52 AM8/12/14
to web...@googlegroups.com
The backbone.js framwork defines the following request type map http://backbonejs.org/docs/backbone.html#section-153
// Map from CRUD to HTTP for our default `Backbone.sync` implementation.
 
var methodMap = {
   
'create': 'POST',
   
'update': 'PUT',
   
'patch':  'PATCH',
   
'delete': 'DELETE',
   
'read':   'GET'
 
};

but when I implement controllers/object.py
@request.restful()
def object():
   
def GET(*args, **vars):
       
from gluon.serializers import json
   
....
   
def PATCH(*args, **vars):
       
'''
        PATCH
        update only a few properties of one object
        '''

       
if request.args(0):
            old_record
= table[getAccess(True)]
       
....

the PATCH method is not working (chrome dev tools output)


So far I could not find the code in gluon that defines the allowed request methods. I would appreciate some help.

Leonel Câmara

unread,
Aug 12, 2014, 12:10:28 PM8/12/14
to web...@googlegroups.com
The problem is probably that in your PATCH function you're accessing request.args(0) when you should be using args[0].

For further help you need to examine the error message you're getting as calling your PATCH is raising TypeError exceptions which usually means something isn't getting the correct arguments.

Falko Delarue

unread,
Aug 12, 2014, 1:11:06 PM8/12/14
to web...@googlegroups.com


On Tuesday, August 12, 2014 6:10:28 PM UTC+2, Leonel Câmara wrote:
The problem is probably that in your PATCH function you're accessing request.args(0) when you should be using args[0].
What? this cannot be right, the request.args(0) is a standard function globally available for a long time, it's web2py standard. Also, where would the args list come from?


For further help you need to examine the error message you're getting as calling your PATCH is raising TypeError exceptions which usually means something isn't getting the correct arguments.
As you may have read I am getting a 400 error not a 500, that means there is no error stack trace. 

Leonel Câmara

unread,
Aug 12, 2014, 1:18:14 PM8/12/14
to
request is available but the restful wrapper cleans request.args and calls the function with them as args, you should not use request.args because request.args have stuff that isn't exactly an argument, namely the method 'PATCH' which is what would be in request.args(0).


> As you may have read I am getting a 400 error not a 500, that means there is no error stack trace. 

That's not exactly true, the thing is, you want the rest API to inform the caller if it's calling it wrong, and that's what web2py thinks it's happening.

Rest actions are run this way:

                try:
                    return rest_action(*_self.args, **getattr(_self, 'vars', {}))
                except TypeError, e:
                    exc_type, exc_value, exc_traceback = sys.exc_info()
                    if len(traceback.extract_tb(exc_traceback)) == 1:
                        raise HTTP(400, "invalid arguments")

There you can see where the HTTP 400 comes from.  

Falko Delarue

unread,
Aug 13, 2014, 5:35:32 AM8/13/14
to web...@googlegroups.com
Ok just I tried this
@request.restful()
def page():  
   
def PATCH(*args, **vars):
       
if args[0]:
           
print 'PATCH args: ', args

But I still get a 400 "Bad Request" error (it does not say 400 "invalid arguments") that why I think it does not even come to this point.
Also request.args() should work since I can use 
    def PUT(*args, **vars):
       
if request.args(0):
           
print request.args(0)
without a problem.

The real problem must be that rest_action or some function much earlier (maybe even in the underlying werbserver) does not accept PATCH requests

Leonel Câmara

unread,
Aug 13, 2014, 7:02:50 AM8/13/14
to web...@googlegroups.com
I've made some confusion with request.args, but the point still stands that you should be using args instead of request.args.

I can tell you the restful decorator doesn't care if it's a PUT or a PATCH as it gets the method from request.env.http_method.

So you're probably on the right track and your webserver may not be accepting it, maybe for overly cautious security configurations. I know nginx has a limit_except option and Apache also has a Limit and LimitExcept directive. I'm not sure if these give you a 400 error so it's also possible that something in your request is exceeding size limits on the webserver and that would give you a 400.
Reply all
Reply to author
Forward
0 new messages