JSON / XML / other as response when raising HTTP error to restful request

52 views
Skip to first unread message

Joel Samuelsson

unread,
Jun 28, 2016, 10:31:48 AM6/28/16
to web2py-users
Hi,

I'd like to respond with a complex data structure when raising a HTTP 409 Conflict as response to a restful request but I can't seem to figure out how. If possible, I'd like to reuse as much as possible from built-in functions in web2py. The best idea I've gotten so far is something like this:

@auth.requires_login()
@request.restful()
def test():
response.view = 'generic.'+request.extension

def GET(*args,**vars):
page = response.render('generic.'+request.extension, dict(errors=["error1", "error2"]))
raise HTTP(409, page)

return locals()


However, this does not set the appropiate Content-Type. Are there easier/better ways to do what I want?

Marlysson Silva

unread,
Jun 28, 2016, 10:44:44 AM6/28/16
to web2py-users
The web2py would use generic views depending of extension of url.
Is it?

Niphlod

unread,
Jun 28, 2016, 11:07:34 AM6/28/16
to web2py-users
or...given that the "big strange thing" is the status .... using the usual syntax with 

response.status = 409

just before before returning  ?

Joel Samuelsson

unread,
Jun 29, 2016, 2:54:53 AM6/29/16
to web2py-users
Marlysson Silva:
Yes, it would. However, when raising an exception as in the example code above, it doesn't.

Niphiod:
Thanks, it was really simple. I've tried other ways of setting the response code but it would either overwrite the body with "409 CONFLICT" or just return 200 OK.
Reply all
Reply to author
Forward
0 new messages