Workaround found.
After digging a little bit more yesterday, we found out the restful
Request handler in gluon/globals.py actually filtered the OPTIONS http verb.
Rejecting it with a 400 http exception. Thus the CORS preflight request
sent by the browser never reached our code in controller.
The browser getting an error when sending OPTIONS request (with the
Access-Control-Request-Method=PUT header for ex.) considers the Cross
origin PUT request is not allowed and then never proceed with the actual
PUT.
So here is what we did:
- In globals.py (v 2.5.1 but also applies to 2.6.3)
diff globals.py ~/git/web2py/gluon/globals.py
153,154c152,153
< if not method in ['GET', 'POST', 'DELETE', 'PUT',
'OPTIONS']:
< raise HTTP(400, "invalid method")
---
> if not method in ['GET', 'POST', 'DELETE', 'PUT']:
> raise HTTP(400, "invalid method")
Then in our controller added handler for the OPTIONS verb also
@request.restful()
def rest():
#import pdb;pdb.set_trace()
response.view = 'generic.'+request.extension
def GET(*args,**vars):
patterns = 'auto'
parser = db.parse_as_rest(patterns,args,vars)
if parser.status == 200:
return dict(content=parser.response)
else:
raise HTTP(parser.status,parser.error)
def POST(table_name,**vars):
return db[table_name].validate_and_insert(**vars)
def PUT(table_name,record_id,**vars):
return db(db[table_name]._id==record_id).update(**vars)
def DELETE(table_name,record_id):
print "DELETE called"
return db(db[table_name]._id==record_id).delete()
def OPTIONS(*args,**vars):
print "OPTION called"
return True
return dict(GET=GET, POST=POST, PUT=PUT, DELETE=DELETE, OPTIONS=OPTIONS)
Any suggestion of how to do without patching web2py, wsgi middleware maybe
?
How about implementing CORS support in a more generic way in web2py ?
Thanks
Bernard (Kanesh's colleague)
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.