REST API Good Practices - nthieu29/icommerce GitHub Wiki
REST API Request
The primary used HTTP verbs are GET, POST, PUT and DELETE.
- GET: Get a list of resources or a single resources.
- POST: Create a new resource.
- PUT: Update/Replace an existing resource.
- DELETE: Delete an existing resource.
We should define our URIs for our service following:
- GET http://localhost:8080/restapi/books - Return a list of all Books.
- GET http://localhost:8080/restapi/books/1 - Return the Book whose ID is 1.
- POST http://localhost:8080/restapi/books - Create a new Book resource.
- PUT http://localhost:8080/restapi/books/1 - Update the Book whose ID is 1.
- DELETE http://localhost:8080/restapi/books/1 - Delete the Book whose ID is 1.
REST API Response
There are really only 3 outcomes in the interaction between an app and an API:
- Everything worked.
- The application did something wrong.
- The API did something wrong.
So we usually use 3 following HTTP codes for the responses:
- 2xx - Successful responses
- 4xx - Client errors - A json with error/more details should return to the client.
- 5xx - Server-side errors - A json with an error should return to the client only when there is no security risk by doing that.