What is an HTTP Method
As part of the HyperText Transfer Protocol, there are different types of requests (methods). The common Internet probably does not know this but they use the GET method every time they submit an address (URL) into the browser. If you're filling out a form on a web page to order new parts for your robot, you are using the POST method to send your data to a server. And when that same user updates their list of videos he has watched RoboTube, he's use a PUT method to modify which videos has watched or not on the RoboTube server.
CRUD
You can use resources to Create, Retrieve, Update, and Delete (CRUD). For each CRUD operation, there is a HTTP method associated with it:
- Create - POST
- Retrieve - GET
- Update - PUT
- Delete - DELETE
Note: Not all requests fall nicely into this model. Some requests nothing to do with a resource. For example, you could turn off your robot remotely. In this case, this is not a CRUD operation.
GET
The GET method retrieves data and do not use bodies in the request. GET methods allows for the use of ids in the URL to get a specific object.
To get data on all robots, your API request should look like this:
GET http://api.website.com/robot
To get data on a particular robot, put the id in the URL like this:
GET http://api.website.com/robot/9000
POST
The POST method is used to create a new resource object. The data to create the new object is contained in the body of the request. To create a new robot object, we would use this request:
POST http://api.website.com/robot
{
...data for new object...
}
PUT
The PUT method is used to modify an existing resource object. The data to modify an existing object is contained in the body of the request. You should use ids in the URL to specify which object to modify.
PUT http://api.website.com/robot/9000
{
...data for updated object...
}
DELETE
The DELETE method is used to remove an existing resource object. DELETE methods do not use bodies and you must specify which object to delete by it's id in the URL.
DELETE http://api.website.com/robot/9000
Documentation in Action
Lets put all this knowledge we have learned so far to use. The following is an example of what a RESTful document may look like for our robot API. This robot has an inventory resource that needs to documented and it has the following methods: retrieving data about a particular item in it's inventory, creating new inventory objects, updating and deleting a particular inventory item, getting a list of all inventory items, and finally a method to tell a particular robot to return to home.
Retrieving Item
Returns data about a specific item.
GET http://api.website.com/robot/9000/inventory/{item id}
Create New inventory
Creates a new collection of inventory items
POST http://api.website.com/robot/9000/inventory
Update Inventory Item
Updates an existing inventory item.
PUT http://api.website.com/robot/9000/inventory/{item id}
Delete Inventory Item
Deletes an inventory item.
DELETE http://api.website.com/robot/9000/inventory/{item id}
Retrieve All Inventory Items
Returns a list of all inventory items.
GET http://api.website.com/robot/9000/inventory
Return Home
Instructs a particular robot to return to home base.
POST http://api.website.com/robot/9000/home