Backbone.js .save() method + TurboGears controller: where are the PUT params?

271 views
Skip to first unread message

Rotem Tamir

unread,
Mar 16, 2012, 12:05:39 PM3/16/12
to TurboGears, mar...@gmail.com
Hi everyone,

I'm using backbone.js on the front to build the user interface, and my
favorite framework on the backend, turbogears 2.1.

I have a controller method for handling object updates by backbone.
Backbone sends object updates as PUT requests (following RESTful
conventions). This is my handler:

@expose('json')
def tasks(self,id=None,**kw):
method = request.environ['REQUEST_METHOD']

if id:
obj = DBSession.query(Task).filter_by(id=id).first()
else:
return dict()

if method=='PUT' and id:
print "PUT"
print kw

Everything works fine, except "kw" is an empty dict. I have tried
looking inside request.environ to find where I can find my params,
which are definatly being sent (I am monitoring my HTTP requests with
firebug.)

I tried to read the RestfulControllers source for clues, but haven't
found any.

Any clues?

Thanks,

Rotem Tamir

Rotem Tamir

unread,
Mar 17, 2012, 7:06:41 AM3/17/12
to TurboGears
it appears that

from simplejson import loads
kw = loads(request.body)

works.

however, i believe that this should just work as if it were a normal
POST request, no?

Rotem Tamir

unread,
Mar 17, 2012, 7:07:05 AM3/17/12
to TurboGears
it appears that

from simplejson import loads
kw = loads(request.body)

works.

however, i believe that this should just work as if it were a normal
POST request, no?

On Mar 16, 6:05 pm, Rotem Tamir <rotemta...@gmail.com> wrote:

Rotem Tamir

unread,
Mar 17, 2012, 7:08:34 AM3/17/12
to TurboGears
it appears that

from simplejson import loads
kw = loads(request.body)

works.

however, i believe that this should just work as if it were a normal
POST request, no?

On Mar 16, 6:05 pm, Rotem Tamir <rotemta...@gmail.com> wrote:

Alessandro Molina

unread,
Mar 17, 2012, 7:21:46 AM3/17/12
to turbo...@googlegroups.com
Are you able to find your parameters inside request.params?
TurboGears looks there for request parameters, it should merge
request.GET and request.POST parameters.
While the names are GET and POST they actually take parameters from
url and body, they are not related to the request method.

> --
> You received this message because you are subscribed to the Google Groups "TurboGears" group.
> To post to this group, send email to turbo...@googlegroups.com.
> To unsubscribe from this group, send email to turbogears+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/turbogears?hl=en.
>

Diez B. Roggisch

unread,
Mar 17, 2012, 8:23:25 AM3/17/12
to turbo...@googlegroups.com

On Mar 17, 2012, at 12:07 PM, Rotem Tamir wrote:

> it appears that
>
> from simplejson import loads
> kw = loads(request.body)
>
> works.
>
> however, i believe that this should just work as if it were a normal
> POST request, no?

No, I don't think so. One could argue that this should be the case, but AFAIK there is no machinery in TG that does this for you.


Diez

Rotem Tamir

unread,
Mar 17, 2012, 9:27:30 AM3/17/12
to TurboGears
I would think that if params submitted via PUT, were treated like GET
or POST params i could use a @validate decorator on them, which is
currently not the case. I'd like to use formencode validators for
RESTful development, I think it would make sense.

Alessandro Molina

unread,
Mar 17, 2012, 10:05:50 AM3/17/12
to turbo...@googlegroups.com
They should actually work.

@expose()
def puttest(self, **kw):
print request.method, kw

outputs:

PUT {'name': u'John', 'location': u'Boston'}

I'm calling the puttest method using jQuery.ajax({'type':'PUT'}) and
the browser reports to be correctly using a PUT request.
Might have I missed something?

alonn

unread,
Mar 17, 2012, 12:11:40 PM3/17/12
to turbo...@googlegroups.com, mar...@gmail.com
in stackoverflow it's stated that :
wsgi represnts put as environ['wsgi.input'] (or something like that)
also not specific TG maybe that would help

Alessandro Molina

unread,
Mar 18, 2012, 8:36:07 AM3/18/12
to turbo...@googlegroups.com
wsgi.input is actually the request body, so it is the same as request.body.

Which version of TG and WebOb are you using? With WebOb 1.0.8 the PUT
parameters end in request.params and so TG is able to use them to
dispatch the request. I didn't try with other versions.

> --
> You received this message because you are subscribed to the Google Groups
> "TurboGears" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/turbogears/-/BgpZGGdLSs4J.

Reply all
Reply to author
Forward
0 new messages