Response is being downloaded instead of rendered by browser

104 views
Skip to first unread message

Matt Wright

unread,
Aug 17, 2011, 5:03:43 PM8/17/11
to we...@googlegroups.com
This is quite strange, but I came back to my app this morning after working on it last night, and the JSON responses on my API end points are suddenly being downloaded by the browser (Chrome, FireFox, Safari) instead of getting rendered as text. Is there a common mistake I may have made?

andrei

unread,
Aug 18, 2011, 8:12:51 AM8/18/11
to web.py
What headers are set?
In my app to return json I do this:

web.header("Content-Type", "application/json")
return json.dumps(result)

Branko Vukelić

unread,
Aug 18, 2011, 12:17:55 PM8/18/11
to we...@googlegroups.com
On 2011-08-18 05:12 -0700, andrei wrote:
> What headers are set?
> In my app to return json I do this:
>
> web.header("Content-Type", "application/json")
> return json.dumps(result)

AFAIK, if you set content type to proper JSON content type, the browser
_will_ download the JSON response instead of dumping it into the window.
To make it dump into the window, I think it's best to first check the
_request_ header, and make sure it's not an XHR response, and then set
the response Content-Type header to 'text/plain'. In case of XHR, you
can leave it as 'application/json'.

The incoming header to check is 'HTTP_X_REQUESTED_WITH', which should be
set to 'XMLHttpRequest'. You set the response content-type to text/plain
if the header is either missing or set to something else than
'XMLHttpRequest'. I forgot the exact API for getting the headers, but
I'm sure you already know how to get them.

--
Branko Vukelic
bra...@herdhound.com
bg.b...@gmail.com

Lead Developer
Herd Hound (tm) - Travel that doesn't bite
www.herdhound.com

Love coffee? You might love Loveffee, too.
loveffee.appspot.com

andrei

unread,
Aug 18, 2011, 1:48:28 PM8/18/11
to web.py
I've did some testing with the latest versions: Firefox and Opera
download, Chrome and Safari display.

Setting web.header("Content-Type", "text/plain") makes them all
display.

On Aug 18, 8:17 pm, Branko Vukelić <bg.bra...@gmail.com> wrote:
> On 2011-08-18 05:12 -0700, andrei wrote:
>
> > What headers are set?
> > In my app to return json I do this:
>
> > web.header("Content-Type", "application/json")
> > return json.dumps(result)
>
> AFAIK, if you set content type to proper JSON content type, the browser
> _will_ download the JSON response instead of dumping it into the window.
> To make it dump into the window, I think it's best to first check the
> _request_ header, and make sure it's not an XHR response, and then set
> the response Content-Type header to 'text/plain'. In case of XHR, you
> can leave it as 'application/json'.
>
> The incoming header to check is 'HTTP_X_REQUESTED_WITH', which should be
> set to 'XMLHttpRequest'. You set the response content-type to text/plain
> if the header is either missing or set to something else than
> 'XMLHttpRequest'. I forgot the exact API for getting the headers, but
> I'm sure you already know how to get them.
>
> --
> Branko Vukelic
> bra...@herdhound.com
> bg.bra...@gmail.com

Branko Vukelić

unread,
Aug 18, 2011, 1:56:33 PM8/18/11
to we...@googlegroups.com
On 2011-08-18 10:48 -0700, andrei wrote:
> I've did some testing with the latest versions: Firefox and Opera
> download, Chrome and Safari display.
>
> Setting web.header("Content-Type", "text/plain") makes them all
> display.

AFAIK, 'text/plain' would be the best bet if the intention is to simply
dump stuff for testing purposes.

Lead Developer

andrei

unread,
Aug 18, 2011, 3:20:46 PM8/18/11
to web.py
I don't use "text/plain" for JSON, its better to use Network tab in
Chrome Developer Toolbar toolbar for JSON debug.

On Aug 18, 9:56 pm, Branko Vukelić <bg.bra...@gmail.com> wrote:
> On 2011-08-18 10:48 -0700, andrei wrote:
>
> > I've did some testing with the latest versions: Firefox and Opera
> > download, Chrome and Safari display.
>
> > Setting web.header("Content-Type", "text/plain") makes them all
> > display.
>
> AFAIK, 'text/plain' would be the best bet if the intention is to simply
> dump stuff for testing purposes.
>
> --
> Branko Vukelic
> bra...@herdhound.com
> bg.bra...@gmail.com

Branko Vukelić

unread,
Aug 18, 2011, 7:55:50 PM8/18/11
to we...@googlegroups.com
On 2011-08-18 12:20 -0700, andrei wrote:
> I don't use "text/plain" for JSON, its better to use Network tab in
> Chrome Developer Toolbar toolbar for JSON debug.

If course, that's the best way. What I meant is _if_ you simply want to
dump the contents into the browser window, then you need to use
text/plain as it works accross all browsers.

Lead Developer

Anand Chitipothu

unread,
Aug 18, 2011, 9:33:39 PM8/18/11
to we...@googlegroups.com
2011/8/18 Branko Vukelić <bg.b...@gmail.com>:

> On 2011-08-18 10:48 -0700, andrei wrote:
>> I've did some testing with the latest versions: Firefox and Opera
>> download, Chrome and Safari display.
>>
>> Setting web.header("Content-Type", "text/plain") makes them all
>> display.
>
> AFAIK, 'text/plain' would be the best bet if the intention is to simply
> dump stuff for testing purposes.

I set the content type to "application/json" by default and change it
to "text/plain" if there is a query parameter text=true.

That makes it easier to debug in the browser.

Anand

Matt Wright

unread,
Aug 19, 2011, 10:35:11 AM8/19/11
to web.py
I'm an idiot. I had a typo in my header. Sorry everyone.

On Aug 18, 9:33 pm, Anand Chitipothu <anandol...@gmail.com> wrote:
> 2011/8/18 Branko Vukelić <bg.bra...@gmail.com>:

Branko Vukelić

unread,
Aug 19, 2011, 10:44:28 AM8/19/11
to we...@googlegroups.com
On 2011-08-19 07:35 -0700, Matt Wright wrote:
> I'm an idiot. I had a typo in my header. Sorry everyone.

Lol. It happens. :)

Reply all
Reply to author
Forward
0 new messages