Rest API best practices
During creating an http api we should remember about few rules.- As a API designers we must know how to name our endpoints. When we have a library which contains users and their books. As a collection we should use a plural form of verb like books and users. Below we can find examples of endpoints for certain activity and strict HTTP methods
- Add new book: /users/{userId}/books/{bookId} POST
- Get book: /users/{userId}/books/{bookId} GET
- Edit book: /users/{userId}/books/{bookId} PUT
- Remove book: /users/{userId}/books/{bookId} DELETE
- Status codes
- 200 OK
- 201 Created
- 202 Accepted - request has been accepted but not completed.
- 204 Non content - the request is correct but no content in response body.
- 400 Bad request
- 401 Unauthorized
- 403 Forbidden
- 404 Not found
- 408 Request time out
- 409 - Conflict - duplicate data or invalid data state would occur.
- 500 Internal server error
- 501 Not implemented
- 503 Service unavailable
Resources:
Comments
Post a Comment