views:

55

answers:

4

If you set the Content-Disposition header to attachment, this causes user-agents to always present a download window for that file. I would like to do this opposite of this: force user-agents to always display the response directly. In this particular case, I have an API that's sending JSON. I'd like to serve it as application/json as indicated by the specification, but since user-agents typically don't recognize the mime type, they present a download window. This makes debugging a real pain in the neck, so I'd like to work around this if I can. Suggestions?

A: 

Content-type: text/plain

AJ
That was most certainly not what I asked. And while pragmatic, it wouldn't be legitimate.
Bob Aman
Something else I've used for debugging JSON in the browser is this plugin: https://addons.mozilla.org/en-US/firefox/addon/10869
AJ
Serving up content as the wrong mime-type can lead to security problems. `text/plain` wouldn't be an issue, but this isn't the kind of thing you want to be careless about. See for example: http://jibbering.com/blog/?p=514
Bob Aman
I don't think this is so bad... If the user agent specifies in the Accept header that it accepts application/json, send that. If not, send it as text/plain. JSON is a subset of text, after all.
Spike Williams
The problem with Accept headers is that many browsers will send a wildcard */* as a last resort (Firefox does), but would still display the download dialog for the content.
Jason
+1  A: 

As an alternative solution, you could add a query string or extension to the URL that, when present, sends it as text/plain instead. That way, you could debug by looking at http://example.com/path/to/json.txt, while real world usage still gets application/json.

From what I've been able to tell, there's not a universally reliable way to always make the content display inline.

Jason
I could also prefix the URI with `view-source:`, but I'd really prefer to be able to debug without thinking about it.
Bob Aman
You could also change the content-type based on whether or not it is an AJAX request.
Jason
+3  A: 

Another method I've used to debug JSON in the browser is the JSONView plugin for Firefox.

AJ
For checking the content of AJAX requests (and much other information), firebug is also helpful (http://getfirebug.com/).
Jason
I already have FireBug, but if you're writing the code for JSON output before the function that uses it, it doesn't really help.
Bob Aman
There's also JSONovich, which performs similarly: http://lackoftalent.org/michael/blog/json-in-firefox/
Bob Aman
Now I just need one for Chrome.
Bob Aman
A: 

I actually took two approaches to this. One, I wrote my own mini-browser so that I could see any response and issue PUT, POST and DELETE requests. The other is to run Fiddler, whilst making the request with the browser. You can see all the details in the request and response with Fiddler.

Darrel Miller
Yeah... no. Not writing my own browser to solve this problem. Also I run Macs and Linux at work, so Fiddler is out too.
Bob Aman