Cheat sheet that will offer developers a reminder of recommended practices done in REST API Development.
|
JSON is the standard for transferring data. Almost every networked technology can use it: JavaScript has built-in methods to encode and decode JSON either through the Fetch API or another HTTP client. Server-side technologies have libraries that can decode JSON without doing much work.
|
|
To make sure that when our REST API app responds with JSON that clients interpret it as such, we should set
Content-Type in the response header to application/json after the request is made. Many server-side app frameworks set the response header automatically. Some HTTP clients look at the Content-Type response header and parse the data according to that format. |
|
Nouns represent the entity that we are trying to recover or manipulate in pathname. Verbs should be avoided because they are already present, they are the HTTP requests types:
GET, POST,DELETE, PUT, etc. |
|
Adding verbs in API pathname doesn't add more value, it makes the pathname even more longer and doesn't help in the description of the API.
|
[GET] /api/books[GET] /api/book[POST] /api/book[GET] /api/getAllBooks[GET] /api/getBook
|
We should name collections with the plural form of the nouns, because it's very common when you want to recover a list of resources to also want to recover one resource from that list and vice versa.
|
|
We use the plural form to be consistent with what is in your database. A table has more than one entity/record and we as developers should name our endpoint in such a way to reflect this, or more formally to be consistent.
|
/api/book/{id} and /api/books/api/account/{id} and /api/accounts/api/person/{id} and /api/persons
https://appgateway.lidlplus.com/app/v19/RO/tickets/list/ where v19 shows the version number.
http://example.com/articles/?sort=+author,-datepublished+ means ascending.- means descending.
|
We should avoid completely putting HTTP information into the response body.
|
|
We don't need to tell the caller of the API in the response body that the API worked with 200 OK or 404 NOT FOUND (etc.).
|
|
Our API should be capable in case of error to fail with an HTTP correct status. And not offer confusing information in the response body like failing with 200 OK
|
{ "status" : "200", "response: "OK", "id" : 1, "name" : Stefan "age" : 25}
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.