Skip to content

Latest commit

 

History

History
48 lines (40 loc) · 1.79 KB

README.md

File metadata and controls

48 lines (40 loc) · 1.79 KB

rest.js

  • Interact with restfull APIs through JavaScript
  • Supports ordinary XHR-requests as well as JSONP
  • No dependencies, minimalistic
  • Compatible with Google closure compiler advanced mode

DEMO

Syntax

  restjs.get(url, callback)
  restjs.post(url, data, callback)
  restjs.put(url, data, callback)
  restjs.delete(url, callback)
  restjs.login(login)       /* set login to use in all requests */
  restjs.password(password) /* set password to use in all requests */
  restjs.baseURL(url)       /* prefix all requests with this url */
  restjs.useJSONP()         /* use JSONP for all requests */
  restjs.useJSON()          /* use normal JSON for all requests */
  restjs.unpack()           /* make get, post, put & del (delete is a reserved word) available as top level methods */

Example

  restjs.get("http://twitter.com/status/user_timeline/ippalix.json?count=10", function(data) {
    alert(data[0].text) /* Text of ippalix latest tweet */
  });

  restjs.get("http://www.flickr.com/services/feeds/photos_public.gne?format=json", function(data) {
    data["items"][0]["media"]["m"] /* => http://farm6.static.flickr.com/5064/xxxxxxxxxx.jpg */
  });

Gotchas

  • Since JSONP is using dynamically injected script-tags, is supports GET (not POST, PUT and DELETE)
  • JSON supports all methods, but doesn't work cross domain(This is why the JSONP "hack" was invented)
  • restjs.unpack() creates the global functions get(), post(), put() and del(). Not delete() since it's a reserved word in javascript
  • No errorhandling just yet, don't make bad API requests ;-)

If You have a REST-api, please consider (enabling cross-origin resource sharing)[http://enable-cors.org/] so we don't have to reside to hacks in the future.

happy apiing,

ippa.