HTTP methods for RESTful services allow you to indicate the operations (create, read, update, and delete -or CRUD- operations) to be applied to a resource.
The result depends on the server implementation.
The data can be predefined or dynamically generated.
The most commonly used HTTP methods are:
This method is used to read or retrieve information.
One of the main characteristics of a GET request is that it should not cause side effects on the server; that is, it should not produce new records nor modify existing ones.
GET is idempotent, which means that no matter how many times you make a GET request, the results will be the same.
GET returns an XML or JSON representation and an HTTP response code: 200 (OK). In case of an error, most of the time, it returns a 404 (NOT FOUND) or 400 (BAD REQUEST) error.
It is used to create new resources (subordinated or not subordinated to another resource).
For example: POST /courses to add a new resource to the courses collection.
POST is not idempotent. So, if you make two identical POST requests, you will probably end up with two resources containing the same information.
POST returns a location header with a link to the newly created resource and HTTP 201 status (CREATED). If the record already exists, it returns a 409 (CONFLICT) error.
It is used to update or create resources.
If you make a PUT request to a known resource URI, you are doing an update.
On the other hand, if the PUT request is to a URI that contains the value of a non-existent resource ID, you will be creating a new resource.
Because it allows both modifying and creating a state on the server, you should use it with caution.
PUT is idempotent. That is, if you create or update a resource using PUT and then make the same call again, the result will be the same.
If you use PUT to update, and all goes well, it returns 200 or 204 (NO CONTENT).
If you use PUT to create, it returns a status HTTP 201. It doesn't return a link, since the client already set the resource ID.
It is used to delete a resource identified by a URI.
DELETE is idempotent. Calling the DELETE method on a resource for a second or more times returns 404 (NOT FOUND).
If the deletion was successful, it returns a 200 status, along with a response body or a status 204 (NO CONTENT).
- In some cases, you must decide the response to return according to the result of the operation.
- In API objects you can use these methods, but the API object itself doesn't control whether or not a record is found.