Thanks to the original writer and article: https://senoritadeveloper.medium.com/rest-api-naming-standards-best-practices-d70ad9b58c66
- POST?— Create
- like form submission, to send data to the server. (PUT?is idempotent,?POST?is not)
- GET?— Read (No Request Body)
- PUT?— Update / Replace (No Response Body)
- Create or replace (can return?200/204/201)
- PATCH?— Update / Modify
- DELETE?— Delete (May Have Request / Response Body But Generally No)
- OPTIONS?— Permitted Communication Operations (No Request Body, Has Response Body)
- HEAD?— Headers (No Request / Response Body), or file size or content length
- TRACE?— Debugging (No Request / Response Body) loopback
- CONNECT?— 2-way communication (SSL/TCP) (No Request Body, Has Response Body)
- 1xx?=> informational
- 2xx?=> success
- 3xx?=> redirection
- 4xx?=> client error
- 5xx?=> server error
Popular Status Codes and Their Reasons:
- 100?-> Continue
- 200?-> OK
- 201?-> Created
- 204?-> No Content
- 301?-> Moved Permanently
- 400?-> Bad Request => validation errors
- 401?-> Unauthorized => when authorization fails
- 403?-> Forbidden
- 404?-> Not Found
- 405?-> Method Not Allowed
- 412?-> Precondition Failed
- 429?-> Too Many Requests => can use for throttling
- 500?-> Internal Server Error
- 503?-> Server Not Available
- Always think about the consumers.
- Use proper HTTP request methods & Return status codes accordingly.
- Prepare both your success and error response models (Exception Handler with “@ControllerAdvice” in Spring Boot).
- Write documentation (Swagger or OpenAPI)
- No secure info in the URI
- Use plurals (can use hyphen separated lower-case )
- /user ?
- /users ??
- /sea-cargo ??
- Use nouns for resources
- /delete-account ?
- DELETE /accounts/845 ??
- Define a consistent approach
- GET /account/256/purchases
- Semicolon, or, more frequently, the comma should be used in lists
- /users/{id1},{id2} ??
- Use camelCase for parameters, Hyphenated-Pascal-Case for HTTP Headers
- Add “Accept” Request Headers
- Support versioning
- in URI as /v1/account or in a header parameter as X-API-VERSION
- HATEOAS?=>?Hypermedia?As?The?Engine?Of?Application?State
- In summary, you send API URLs in the response for the consumer to use for showing the next steps (actions) from there. Like, on a web page, you list products and click on a product to get its details. In your list of products’ API responses, you also return GET API URLs for each product so the front end does not have to build up the URLs.
- loose coupling?=> If a consumer of a REST service needs to hard-code all the resource URLs, then it is tightly coupled with your service implementation.
I am here just to share any new knowledge with you so keep reading every day and happy learning!
Senior Software Engineer ? Full Stack ? JavaScript ? PHP ? MySQL ? AWS
1 年The best URL should be neat, elegant, and simple so that developers using your product can easily use them in their applications.