March 02, 2011

PUT and DELETE in call.jsonlib.com

My son was trying to use call.jsonlib.com to access the Google Docs API and told me he needed the ability to execute HTTP PUT and DELETE, not just GET and POST.

Done. Now call.jsonlib.com/fetch supports a "&method=" CGI argument that you can set to PUT or DELETE, and Anthony reports that it's working fine for accessing the API. (Does anybody know of any simple public HTTP servers out there that will echo your request so that it can be easily tested? At most URLs, using these unusual methods will just give you a 405 error.)

Access-Control-Allow-Origin

Another change is that call.jsonlib.com now supports cross-domain access via XHR (not just jsonp) on browsers that support the Access-Control-Allow-Origin header.

The problem with JSONP is that you can only use the GET method, not POST, so all of your submitted data must fit in a URL string. Some browsers, servers, and proxies may have trouble handling URL strings longer than about 2K, so this means you cannot use JSONP to reliably submit long chunks of data.

The solution is to go back to using traditional XHR techniques, but to tell your browser that cross-origin access is OK using the Access-Control-Allow-Origin header. This header is supported on modern browsers (FF, Chrome, Safari, Opera, and IE8+).

You can write code like the following (jQuery) to use the POST method to interact with jsonlib:

$.post('http://call.jsonlib.com/fetch',
  { url: 'http://davidbau.com/data/animals' },
  function(m) { output(m); },
  'json');

The above just uses POST to talk to jsonlib, and asks jsonlib to do a GET, but obviously you can use a POST to ask jsonlib to do a POST or a PUT as well.

Sniffing Character Encodings

The other update is logic for sniffing encodings. Since call.jsonlib.com always returns its contents as JSON unicode strings, it needs to understand how to convert responses from arbitrary other encodings. For text documents, it now uses BeautifulSoup and the chardet library to automatically detect encodings and convert text and html documents to unicode. This behavior can be overridden using the &encoding= CGI argument.

Examples: fetching utf-8 smart quotes and fetching cp-1252 smart quotes.

Non-text documents are treated as iso-8859-1 which is the simplest way use unicode 0-255 codepoints as a way of storing a bytestream (example: fetching a jpeg.)

Posted by David at 06:52 AM | Comments (2)