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:

javascript
< view plain text >
  1. foo({
  2.     "status_code": 200,
  3.     "ok": true,
  4.     "headers": {
  5.         "Via": "HTTP\/1.1 GWA",
  6.         "Content-Encoding": "gzip",
  7.         "Accept-Ranges": "bytes",
  8.         "X-Google-Cache-Control": "remote-fetch",
  9.         "Vary": "User-Agent",
  10.         "Last-Modified": "Tue, 29 Jul 2008 16:03:20 GMT",
  11.         "Connection": "close",
  12.         "Cache-Control": "private",
  13.         "Date": "Tue, 29 Jul 2008 16:23:14 GMT",
  14.         "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\"",
  15.         "Content-Type": "text\/html; charset=utf-8",
  16.         "X-XRDS-Location": "http:\/\/open.login.yahooapis.com\/openid20\/www.yahoo.com\/xrds"
  17.     }
  18. })

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:

javascript
< view plain text >
  1. {
  2.     "status_code": 404,
  3.     "ok": true,
  4.     "headers": {
  5.         "Via": "HTTP\/1.1 GWA",
  6.         "X-Google-Cache-Control": "remote-fetch",
  7.         "Vary": "User-Agent",
  8.         "Connection": "close",
  9.         "Cache-Control": "private",
  10.         "Date": "Tue, 29 Jul 2008 16:25:31 GMT",
  11.         "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\"",
  12.         "Content-Type": "text\/html; charset=utf-8"
  13.     }
  14. }

You could for example use this to write a script that removes links from the document when they are broken.

Related Content:

  • SSL VPN use is all about security
    A recent Infonetics poll of 250 companies found that security, not cost or easier user access, is still the biggest driver for deploying SSL...
  • SSL VPN use is all about security
    A recent Infonetics poll of 250 companies found that security, not cost or easier user access, is still the biggest driver for deploying SSL...
  • Empowering users: It is not all about BYOD
    The real benefits of bring your own device (BYOD) only arise when the right culture and mindset are...
  • It's all about service
    Bad weather is no stranger to North-Western Europe during the cold, dark, stormy months of winter. Come to that, each of the seasons experience its...
  • WCF is about SOA
    The relationship between Windows Communication Foundation (WCF) and service-oriented architecture (SOA) is detailed in one of the chapters of...

Posted by Chris Heilmann at 11:27 am
8 Comments

+++--
3.5 rating from 16 votes

8 Comments »

Comments feed TrackBack URI

name one good practice where this comes in use…
Rather useless if u ask me…

Comment by V1 — July 29, 2008

I think I did…

Comment by Chris Heilmann — July 29, 2008

U got analytic and 404 error logs for that..

Comment by V1 — July 29, 2008

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.

Comment by ragjunk — July 29, 2008

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.

Comment by SimonWillison — July 29, 2008

V1: This could be useful when working with CouchDB.

Comment by tapwater — July 29, 2008

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&ltmpl=homepage&rm=false

I assumed json-head would return 302, which is what I see in Fiddler2.

Comment by ragjunk — July 29, 2008

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

Comment by SimonWillison — July 29, 2008

Leave a comment

You must be logged in to post a comment.