Network concept with metallic items connected on black background - 3D rendering illustration
0 0
Read Time:1 Minute, 43 Second

REST stands for Representational State Transfer.

Any API (Application Programming Interface) that follows the REST design principle is said to be RESTful.

Simply put, a REST API is a medium for two computers to communicate over HTTP (Hypertext Transfer Protocol), in the same way clients and servers communicate.

Use JSON as the Format for Sending and Receiving Data

To ensure the client interprets JSON data correctly, you should set the Content-Type type in the response header to application/json while making the request.

GET would retrieve data, POST will create data, PUT will update data, and DELETE will get rid of the data.

Name Collections with Plural Nouns

So, instead of https://mysite.com/post/123, it should be https://mysite.com/posts/123.

Use Status Codes in Error Handling

STATUS CODE RANGEMEANING
100 – 199Informational Responses.
For example, 102 indicates the resource is being processed
300 – 399Redirects
For example, 301 means Moved permanently
400 – 499Client-side errors
400 means bad request and 404 means resource not found
500 – 599Server-side errors
For example, 500 means an internal server error
STATUS CODE

Use Nesting on Endpoints to Show Relationships

For example, in the case of a multi-user blogging platform, different posts could be written by different authors, so an endpoint such as https://mysite.com/posts/author would make a valid nesting in this case.

In the same vein, the posts might have their individual comments, so to retrieve the comments, an endpoint like https://mysite.com/posts/postId/comments would make sense.

You should avoid nesting that is more than 3 levels deep as this can make the API less elegant and readable.

Use Filtering, Sorting, and Pagination to Retrieve the Data Requested

Filtering, sorting, and pagination are all actions that can be performed on the collection of a REST API. This lets it only retrieve, sort, and arrange the necessary data into pages so the server doesn’t get too occupied with requests.

Be Clear with Versioning

https://mysite.com/v1/

https://mysite.com/v2/

References

https://www.freecodecamp.org/news/rest-api-best-practices-rest-endpoint-design-examples/

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

About Author

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *