I hope that the new version can join the support of REST

11 views
Skip to first unread message

jejw...@gmail.com

unread,
Jan 29, 2008, 12:35:08 AM1/29/08
to web2py Web Framework
I hope that the new version can join the support of REST

Massimo Di Pierro

unread,
Jan 29, 2008, 12:40:51 AM1/29/08
to web...@googlegroups.com
It is not a priority of mine since I do not use it but I am open to
the possibility. Convince me there is a need.
Since I do not know REST, it is also not obvious that web2py does not
work with REST now.

Massimo

voltron

unread,
Jan 29, 2008, 2:47:12 AM1/29/08
to web2py Web Framework
I am also aiming to get something RESTful done very soon with Web2PY.
Soneone else and I have asked the same question before, here are links
to the posts, they could be helpful for you.


http://groups.google.com/group/web2py/browse_thread/thread/d9f7919c16d62514/a97407599bf7a815?lnk=gst&q=REST#a97407599bf7a815

http://groups.google.com/group/web2py/browse_thread/thread/613d4ac6f87ad413/856d8f79bdb67f57?lnk=gst&q=REST#856d8f79bdb67f57


If you take a look at how other frameworks provide support for REST, I
think it would not be hard to re-implement them in Web2py. You can
construct your interfaces in the Web2py controllers and call them
using a parameter sent by a form, for example "FORM(db.users,
_rest_method = 'PUT')". I intended to write a mini tutorial on this
once I get a stable and working example done


On Jan 29, 6:40 am, Massimo Di Pierro <mdipie...@cs.depaul.edu> wrote:
> It is not a priority of mine since I do not use it but I am open to
> the possibility. Convince me there is a need.
> Since I do not know REST, it is also not obvious that web2py does not
> work with REST now.
>
> Massimo
>

mdipierro

unread,
Jan 29, 2008, 3:11:35 AM1/29/08
to web2py Web Framework
thank you voltron.

mdipierro

unread,
Jan 29, 2008, 3:54:17 AM1/29/08
to web2py Web Framework
I played with this a little. web2py has no problem with REST URIs but
I do not know how to generate a PUT/DELETE request. I tried with

<form method="PUT">...</form>

but the server received it as a GET. Not sure if the httpserver is
filtering it out or it the browser is.

Does anybody have any idea?

voltron

unread,
Jan 29, 2008, 4:59:01 AM1/29/08
to web2py Web Framework
PUT, HEAD and DELETE are not supported by HTML Forms. This would be
changed as from HTML version 5. The only way to simulate this is as I
have mentioned above, you can "overload" POST or GET and send another
parameter with the right method, for example PUT, you read and use
this then in your controllers

voltron

unread,
Jan 29, 2008, 5:01:11 AM1/29/08
to web2py Web Framework
Very rough example:

FORM(method= "GET", _my_rest_method = "PUT")

Massimo Di Pierro

unread,
Jan 29, 2008, 12:30:07 PM1/29/08
to web...@googlegroups.com
Now I get it. You can do that now. Here is how.

create an empty model called rest.py and write in it:

def REST(form,**attributes):
    for name,value in attributes.items():
        form.components.append(INPUT(_type="hidden",_name=name,_value=value))
    return form

place it in any app that wants to do rest, no need to import anything.

Now write a test controller like the one below

def test():
    form=FORM('your name:',INPUT(_name='name'),INPUT(_type='submit'))
    form=REST(form,method='PUT')
    if form.accepts(request.vars,session):
        if form.vars.method=='PUT':
            response.flash=' you jut did a REST PUT!'
    return dict(form=form)

Not sure that there is anything else (protocol-wise) in rest other than software engineering.

voltron, if this solves your problem, would you be willing to write a mini-howto?

Massimo

voltron

unread,
Jan 29, 2008, 1:04:24 PM1/29/08
to web2py Web Framework

Hi Massimo,

yes, I'll be glad to write a mini-tutorial, I will try to test your
code tommorow. REST does not do anything else protocol-wise because it
uses HTTP, or did you mean modification of headers or the content of
the response? Very interesting code you posted. This is another thing
that could be put in a global library as I mentioned a few posts down,
Ill put it in my "lib" appliance with my other global modules :-)

Massimo Di Pierro

unread,
Jan 29, 2008, 1:12:43 PM1/29/08
to web...@googlegroups.com
You can make your on library, create an app call it "library" and put
in these your shared models, helpers, static files, etc BUT no
controllers (unless you want to post examples).

Stuff I maintain is in gluon.contrib. Perhaps this helper should be
in gluon.contrib.... after your mini-review.

Massimo

voltron

unread,
Jan 30, 2008, 3:25:29 AM1/30/08
to web2py Web Framework
Okay, the battle is half-won I think. REST URL resource naming is very
important, Web2py still has the problem that it still is very
inflexible with its URL naming scheme, thus making it almost
impossible to create good resources. Her is an example taken from this
nice tutorial (http://www.xfront.com/REST-Web-Services.html)

This resource:

http://www.parts-depot.com/parts

resolves to this in Web2py

http://www.parts-depot.com/parts/default/index

This problem does not stop there, the URLs are simply not "clean"
looking and misleading

voltron

unread,
Jan 30, 2008, 3:37:33 AM1/30/08
to web2py Web Framework
A workaround that one could implement is to create a sort of
"umbrella" application, so the above mentioned resource would then be:

http://www.parts-depot.com/warehouse/parts

But this does not work for all resource URLs

Massimo Di Pierro

unread,
Jan 30, 2008, 11:02:52 AM1/30/08
to web...@googlegroups.com
I cannot access you urls.

Massimo

voltron

unread,
Jan 30, 2008, 1:41:13 PM1/30/08
to web2py Web Framework
Oh, those are examples from the tutorial, its just to how how they are
formed, not the content that they link to

Massimo Di Pierro

unread,
Jan 31, 2008, 11:12:04 AM1/31/08
to web...@googlegroups.com
There is a new version in the trunk and I found a better way to do REST without a new helper. 
Please try it with the example below:


def test():
    form=FORM('your name:',INPUT(_name='name'),INPUT(_type='submit'),hidden=dict(method="PUT'))
    if form.accepts(request.vars,session):
        if form.vars.method=='PUT':
            response.flash=' you jut did a REST PUT!'
    return dict(form=form)

Works with SQLFORM too. Voltron, please use this in yout howto.

Massimo
Reply all
Reply to author
Forward
0 new messages