Tuesday, July 29th, 2008
JSON-head, it is not about size – it is about usefulness
<p>Simon Willison is lately having a lot of fun with App Engine and developing small RESTful helper apps for the masses out there. Following JSON Time, a small timezone lookup API over HTTP (showcased at BBC's Mashed earlier this year) he now released JSON head which is what it says on the tin: a HTTP HEAD lookup app that returns a JSON object.If you for example use the APP with a url parameter like this:
http://json-head.appspot.com/?url=http://www.yahoo.com/&callback=foo
You are going to get a JSON response with a wrapper function called foo:
-
-
foo({
-
"status_code": 200,
-
"ok": true,
-
"headers": {
-
"Via": "HTTP/1.1 GWA",
-
"Content-Encoding": "gzip",
-
"Accept-Ranges": "bytes",
-
"X-Google-Cache-Control": "remote-fetch",
-
"Vary": "User-Agent",
-
"Last-Modified": "Tue, 29 Jul 2008 16:03:20 GMT",
-
"Connection": "close",
-
"Cache-Control": "private",
-
"Date": "Tue, 29 Jul 2008 16:23:14 GMT",
-
"P3P": "policyref="http://p3p.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"",
-
"Content-Type": "text/html; charset=utf-8",
-
"X-XRDS-Location": "http://open.login.yahooapis.com/openid20/www.yahoo.com/xrds"
-
}
-
})
-
If you try a non-existing URL like:
http://json-head.appspot.com/?url=http://www.yahoo.com/nada
You'll get the 404 error message in JSON:
-
-
{
-
"status_code": 404,
-
"ok": true,
-
"headers": {
-
"Via": "HTTP/1.1 GWA",
-
"X-Google-Cache-Control": "remote-fetch",
-
"Vary": "User-Agent",
-
"Connection": "close",
-
"Cache-Control": "private",
-
"Date": "Tue, 29 Jul 2008 16:25:31 GMT",
-
"P3P": "policyref="http://p3p.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"",
-
"Content-Type": "text/html; charset=utf-8"
-
}
-
}
-
You could for example use this to write a script that removes links from the document when they are broken.








name one good practice where this comes in use…
Rather useless if u ask me…
I think I did…
U got analytic and 404 error logs for that..
I have a use case where an URL is requested in an iframe. If the HTTP response contains a 301 or 302, IE does a redirect, which replaces the “entire” page, not just the iframe content. There is no way to overcome this issue, at least to my knowledge. With this library, I think, I can examine the status_code and set the iframe’ src to the original URL if the status code is 200 and to the redirected URL, if it is 301 or 302.
Here’s one example (the reason I built it in fact) – I’m creating an interface for constructing podcast RSS feeds, which needs to include the length of each mp3 file in bytes in the enclosure element. The MP3s are online already. When the user pastes the URL to the MP3 in to the admin application, I can unobtrusively use json-head to find the Content-Length of that file and populate the “size” field in the interface for them. Because it uses JSON-P I can call the script from anywhere, and because it’s on App Engine I don’t have to worry about hosting a potentially expensive operation on my own server.
V1: This could be useful when working with CouchDB.
Actually, it didn’t help. Running:
http://json-head.appspot.com/?url=http://docs.google.com
produced a status_code of 200. If you run, http://docs.google.com directly, you will be redirected to:
https://www.google.com/accounts/ServiceLogin?service=writely&passive=true&nui=1&continue=http%3A%2F%2Fdocs.google.com%2F&followup=http%3A%2F%2Fdocs.google.com%2F<mpl=homepage&rm=false
I assumed json-head would return 302, which is what I see in Fiddler2.
ragjunk: I consider that a bug, but unfortunately the App Engine urlfetch API automatically follows HTTP redirects: http://code.google.com/appengine/docs/urlfetch/overview.html
There are a couple of relevant tickets in their issue tracker; I just posted a comment on this one: http://code.google.com/p/googleappengine/issues/detail?id=363