Learning to REST

The free practice RESTful API brought to you by LearnCode.academy…oh snap

How it works

…now there’s 3 friends.

  • What? How did it know my username and collection existed? If it doesn’t exist, it creates a blank collection for you to post to.
  • What’s the catch? 1) All data resets nightly. 2) Anybody can modify anything…this ain’t for your production app, baby.

What should my Javascript code look like?

  • Get a collection: GET /api/:user/:collection

    (Choose any username except learncode and collection name you like)

    $.ajax({ type: ‘GET’, url: ‘http://rest.learncode.academy/api/johnbob/friends’, success: function(data) { console.log(“I have friends!”, data); //returns all of johnbob’s friends } });

  • Add a new item: POST /api/:user/:collection

    (Collection is created when you post your first item)

    $.ajax({ type: ‘POST’, url: ‘http://rest.learncode.academy/api/johnbob/friends’, data: {name: ‘Billy Bob’, age: 27}, success: function(data) { console.log(“Friend added!”, data); //the new item is returned with an ID } });

  • Get a single item: GET /api/:user/:collection/:id

    $.ajax({ type: ‘GET’, url: ‘http://rest.learncode.academy/api/johnbob/friends/1’, success: function(data) { console.log(“Here’s a friend!”, data); //returns friend id#1 } });

  • Update an item with new data: PUT /api/:user/:collection/:id

    $.ajax({ type: ‘PUT’, data: {name: ‘Billy Bob’, age: 28}, url: ‘http://rest.learncode.academy/api/johnbob/friends/1’, success: function() { //no data…just a success (200) status code console.log(‘Friend Updated Successfully!’); } });

  • Delete an item: DELETE /api/:user/:collection/:id
    $.ajax({
      type: 'DELETE',
      url: 'http://rest.learncode.academy/api/johnbob/friends/1',
      success: function() {
        //no data...just a success (200) status code
        console.log('Friend Deleted Successfully!');
      }
    });

Comments

comments

%d bloggers like this: