Response API

13 views
Skip to first unread message

Jussi Arpalahti

unread,
Apr 13, 2011, 4:10:46 PM4/13/11
to aspen-users
Hi.

I'm trying to set the response object to something different than what
is in the simplate's template page. Aspen's behaviour does not seem to
match the documentation.

If I raise a Response, I get the standard error template with the
error code:

from aspen import Response
Response.body='content'
raise Response

=> Response is Ok, program!


Trying to create a new response as a response variable does not
prevent the template part being run:

response = Response(200,'content')
response


In the docs it's said that the variable named response should be
returned as the response object. I tried looking at the simplates
code, but could not figure out how the response's namespace is build.


Thanks,
Jussi

Chad Whitacre

unread,
Apr 15, 2011, 1:00:47 AM4/15/11
to Jussi Arpalahti, aspen-users
Jussi,

Yeah, sounds like a bug. I've created an issue:



chad




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


Chad Whitacre

unread,
Apr 15, 2011, 1:19:35 AM4/15/11
to Jussi Arpalahti, aspen-users
Jussi,
 
Yeah, sounds like a bug. I've created an issue:

I take this back. :)

 
from aspen import Response
Response.body='content'
raise Response

This is raising the Response *class*, which is actually being instantiated by Python's try/except machinery with default values. This is then caught by Aspen's "nice error handling" machinery (which I haven't documented yet), and you are getting the "Ok, program!" output. Make sense?

Instead what you need to do here is raise an *instance* of Response, like so:

    from aspen import Response
    ^L
    raise Response(200, 'content')
    ^L

Note that this will still go through the nice error handling machinery; the raise feature is really designed for non-200 responses. If you want to avoid the error handling machinery, you should do what you tried next, letting the response object "fall off the end" of the Python page.


Trying to create a new response as a response variable does not
prevent the template part being run:

response = Response(200,'content')
response

And here this is the expected behavior. If you want to skip the template page, you have to raise a Response instance.

Maybe you want to be able to raise a 200 response and not have it go through the error machinery, though. Is this the case? Could you maybe say more about your use case?



chad

Chad Whitacre

unread,
Apr 15, 2011, 2:17:58 AM4/15/11
to Jussi Arpalahti, aspen-users
Jussi,

Looking at the Response object docs, I can see how they were confusing. I've gone ahead and cut a new release with tonight's bug fixes. This release includes beefed up docs on Response and a section on unicode in template pages:



chad

Jussi Arpalahti

unread,
Apr 15, 2011, 5:51:44 AM4/15/11
to aspen-users
Well, I was thinking that sometimes I don't need the template part.
Just some simple message like "not authorized for this action". Then I
won't need to complicate the template code with error messages and
such. Of course I could redirect to a different simplate, but that's
not always very REST-like.

I still don't understand this sentence:
"Whatever is named “response” at the end of page two will be what
Aspen uses to capture the template output and respond to the client."

Do you mean, that the variable named "response" should be a Response
object and it will be used as a response, but its body attribute will
be replaced with the output of the template page?

Jussi

Chad Whitacre

unread,
Apr 15, 2011, 10:37:38 AM4/15/11
to Jussi Arpalahti, aspen-users
Jussi,

Well, I was thinking that sometimes I don't need the template part.
Just some simple message like "not authorized for this action". Then I
won't need to complicate the template code with error messages and
such. Of course I could redirect to a different simplate, but that's
not always very REST-like.

Yes, this is the perfect case for raising a Response object (i.e., an instance). So for HTTP's Not Authorized you would raise Response(403).

 
I still don't understand this sentence:
"Whatever is named “response” at the end of page two will be what
Aspen uses to capture the template output and respond to the client."

Do you mean, that the variable named "response" should be a Response
object and it will be used as a response, but its body attribute will
be replaced with the output of the template page?

That's exactly it. I've reworked the docs to include your wording and indicate that this is an advanced use case:



Thanks Jussi!




chad
Reply all
Reply to author
Forward
0 new messages