Writing content type 'application/json' to response

10,140 views
Skip to first unread message

Jamie Forrest

unread,
May 18, 2011, 11:50:29 AM5/18/11
to Tornado Web Server
According to the tornado/web.py source code (and everything else I
could find online), if you pass the self.write method a dictionary,
tornado will "write it as JSON and set the Content-Type of the
response to be text/javascript."

However, when I run curl -I on a url where I'm doing this, the content
type is still set to 'text/html.' In any case I'd prefer to set the
content type to 'application/json' which makes more sense for what I'm
doing. Is there a more granular way to control the content type of the
response?

David Birdsong

unread,
May 18, 2011, 1:09:48 PM5/18/11
to python-...@googlegroups.com

in your tornado.web.RequestHandler, manually set the content_type
before calling self.write
self.content_type = 'application/json'

Jamie Forrest

unread,
May 18, 2011, 2:30:59 PM5/18/11
to Tornado Web Server
> in your tornado.web.RequestHandler, manually set the content_type
> before calling self.write
> self.content_type = 'application/json'

On further inspection it just appears that when I was running curl -I
it was causing the server to return a 405, whose content type is
always 'text/html'. If I run curl -i (lowercase) then I don't get the
405. However, now I am seeing 'text/javascript' as the content type.
Calling self.content_type = 'application/json' as David suggests does
not seem to update the content type (possibly because the self.write
method in web.py is where the content type is getting changed to 'text/
javascript'?) Is there any way to set this w/o monkey patching web.py?

Matt Ferguson

unread,
May 18, 2011, 2:33:05 PM5/18/11
to python-...@googlegroups.com
self.set_header("Content-Type", "application/json") might do it for you

Matt Ferguson

unread,
May 18, 2011, 2:35:20 PM5/18/11
to python-...@googlegroups.com
Nevermind, I see it. https://github.com/facebook/tornado/blob/master/tornado/web.py#L424

You could call the JSON encode function yourself so you're passing it a string, not a dict, and then set the right header manually.

On May 18, 2011, at 11:30 AM, Jamie Forrest wrote:

Jamie Forrest

unread,
May 18, 2011, 2:35:37 PM5/18/11
to Tornado Web Server
On May 18, 2:33 pm, Matt Ferguson <mbf...@gmail.com> wrote:
> self.set_header("Content-Type", "application/json") might do it for you

Thanks Matt. This worked, but only when I placed it after the
self.write call.

Jamie Forrest

unread,
May 18, 2011, 6:04:35 PM5/18/11
to Tornado Web Server
On May 18, 2:35 pm, Matt Ferguson <mbf...@gmail.com> wrote:
> Nevermind, I see it.170https://github.com/facebook/tornado/blob/master/tornado/web.py#L424
>
> You could call the JSON encode function yourself so you're passing it a string, not a dict, and then set the right header manually.

For what it's worth I submitted a pull request on GitHub with a patch
that lets you can optionally pass a dict of header values into
self.write (I also removed the magical content-type setting when the
chunk is an instance of dict).
Reply all
Reply to author
Forward
0 new messages