Learning to REST
The free practice RESTful API brought to you by LearnCode.academy…oh snap
How it works
-
- Get any collection at /api/:user/:collection /api/learncode/friends (pick any username and any collection name)
- Get a specific item by adding an id /api/learncode/friends/1
- Post a new friend {name: ‘Superman’, age: 29} to /api/learncode/friends and refresh /api/learncode/friends (pick any username and any collection name)
…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!'); } });