After learning the basics regarding REST and RESTful URIs Topher decided he was ready to learn more about HTTP requests. One of the important characteristics of a RESTful Web Service is the use of a Uniform Interface in which it uses the four HTTP methods in a way consistent with their definition. These methods are classified as either safe, idempotent, or neither. A method classified as safe is a method that is intended only for information retrieval and should not change the state of any resource on the server. In other words a safe request should be free from any side effects . A method classified as idempotent means that multiple or n identical requests should have the same effect as a single request.
Let’s look more closely at these types of requests.
Method | Intended Use | Safe | Idempotent |
---|---|---|---|
GET | To retrieve a resource | Yes | Yes |
POST | To create a resource | No | No |
PUT | To change the state of or update a resource | No | Yes |
DELETE | To remove or delete a resource | No | Yes |
GET
The GET request is used by the client to retrieve a resource or execute a query that will allow the server to retrieve a set of matching
resources. A GET request should be free of side effects, meaning it does not change the state of any resource on the server.
A GET request URI looks like
GET http://www.bti360.com/dictionary/set GET http://www.bti360.com/dictionary?synonym=happy
The first URI is a URI for the definition of set. This request will return the resource with “set” as the identifier. The second URI is a URI for a query for words in the dictionary that are synonyms for happy. This request may return multiple resources such as “content, joyful, chipper, peppy” and the resources are returned to the client in the Response Body.
POST
The POST request is used to create a resource on the server. A POST request URI looks like
POST http://www.bti360.com/dictionary
This URI looks very similar to the GET URI except that it contains no resource identifiers or a query string. Dictionary is the parent resource and a resource create with this POST request will be stored as a child resource of dictionary. With a POST request the data describing the resource is sent along with the request in something called the Request Body. A POST is neither safe nor idempotent because by definition the request creates a resource on the server, and multiple requests could create multiple resources.
PUT
The PUT request is used to update or modify a resource on the server. A PUT request URI looks like
PUT http://www.bti360.com/dictionary/set
You may be thinking “well that looks very similar to a POST request” and you would be right. The PUT request looks identical to the POST request with one main difference. With a POST request the server generates a unique identifier for the new resource and for a PUT the unique identifier is already generated and known by the client. If the client was allowed to generate the id then there would be no guarantee that the id was unique.
DELETE
The DELETE request is used to delete or remove a resource from the server. A DELETE request URI looks like