setting contents-type

202 views
Skip to first unread message

Alex Greif

unread,
Mar 1, 2008, 11:46:40 AM3/1/08
to web.py
Hi,
I just started to use webpy and have the following
question:
my requests are forwarded to the webpy app
from an apache through the mod_wsgi. I realize that the
response header (content-type) is not set
automatically. So I have to set in every GET method the
header with web.header(). Is there a way to define the
header contents in one global place or do I have to set
it in every GET methods?

Thanks,
Alex.

slav0nic

unread,
Mar 2, 2008, 10:04:23 AM3/2/08
to web.py
def setheader():
web.header("Content-Type","text/html; charset=utf-8")

web.loadhooks['setheader'] = setheader

Ben Hoyt

unread,
Mar 2, 2008, 1:59:02 PM3/2/08
to we...@googlegroups.com
Another way is on the __init__ method of the request class, which gets called by web.py when the class is instantiated. This way you can do things based on the request class type. For instance, I often use classes Html and Text, like so:

class Request:
    # base request class, set content_type in sub-class
    def __init__(self):
        web.header('Content-Type', self.content_type)

class Html(Request):
    content_type = 'text/html'

class Text(Request):
    content_type = 'text/plain'

class home(Html):
    def GET(self):
        print '<b>This will be an HTML request</b>.'

class json(Text):
    def GET(self):
        print '["This will be a JSON one."]'

-Ben

2008/3/3 slav0nic <slav...@gmail.com>:



--
Ben Hoyt, http://benhoyt.com/

Yoan Blanc

unread,
Mar 2, 2008, 2:09:27 PM3/2/08
to we...@googlegroups.com
apart that JSON mimetype is: application/json
(http://www.ietf.org/rfc/rfc4627.txt)

Anyway, this is a nice trick.

2008/3/2 Ben Hoyt <ben...@gmail.com>:

bubblboy

unread,
Mar 2, 2008, 2:12:23 PM3/2/08
to we...@googlegroups.com
Ben Hoyt wrote:
> Another way is on the __init__ method of the request class, which gets
> called by web.py when the class is instantiated. This way you can do
> things based on the request class type. For instance, I often use
> classes Html and Text, like so:
>
> class Request:
> # base request class, set content_type in sub-class
> def __init__(self):
> web.header('Content-Type', self.content_type)
>
> class Html(Request):
> content_type = 'text/html'
>
> class Text(Request):
> content_type = 'text/plain'
>
> class home(Html):
> def GET(self):
> print '<b>This will be an HTML request</b>.'
>
> class json(Text):
> def GET(self):
> print '["This will be a JSON one."]'
>
> -Ben
>
> 2008/3/3 slav0nic <slav...@gmail.com <mailto:slav...@gmail.com>>:

>
>
> def setheader():
> web.header("Content-Type","text/html; charset=utf-8")
>
> web.loadhooks['setheader'] = setheader
>
> On 1 мар, 18:46, Alex Greif <alex.gr...@gmail.com
> <mailto:alex.gr...@gmail.com>> wrote:
> > Hi,
> > I just started to use webpy and have the following
> > question:
> > my requests are forwarded to the webpy app
> > from an apache through the mod_wsgi. I realize that the
> > response header (content-type) is not set
> > automatically. So I have to set in every GET method the
> > header with web.header(). Is there a way to define the
> > header contents in one global place or do I have to set
> > it in every GET methods?
> >
> > Thanks,
> > Alex.
>
>
>
>
> --
> Ben Hoyt, http://benhoyt.com/

That one is nice :) I personally use decorators, it's a little more work
but it does the job as well ^^

Alex Greif

unread,
Mar 3, 2008, 3:10:43 AM3/3/08
to we...@googlegroups.com
Hi bubblboy,

would it be possible, that you post a small sample with the decorators?
Then this thread would contailn some good samples how to do it.

thanks,
Alex.

--
alex....@gmail.com

Reply all
Reply to author
Forward
0 new messages