Reputation: 159
As per the JSON API Specification
http://example.com/articles/1/comments
http://example.com/articles/1/author
articles/1?include=comments,author
Does it is mandatory to follow and specification for REST API
?
Upvotes: 0
Views: 564
Reputation: 6338
It depends. All the three requests you have listed are supported by the JSON:API specification.
The specification defines two ways to fetch related resources:
If you need the related resources immediately, it's recommended to fetch them together with the primary resource in a single request. This avoids sequential requests, which slows down your application's time to first meaningful render.
If you don't know yet, if the related resources will be needed at all, it's recommended to fetch them later if needed. Fetching them with the initial request would slow down your application's time to first meaningful render without any benefit. You could fetch them later when needed using a related resource link provided by the server with the primary resource(s).
Typically there is no need to fetch related resources for different relationships in a single request. You could fetch them independently from each other in parallel. Therefore the specification does not provide a way to fetch related resources of different relationships in a single request - unless including them with the primary data.
Please note that you could include related resources with a related resource link as well. E.g. you may fetch an author of a post and include all other posts of that author in the request. Following your examples from above, such a request may look like this: GET http://example.com/articles/1/author?include=posts
Of course it's not mandatory to follow a specific specification when implementing a software. You could implement a HTTP API without following the REST API design pattern. You could implement a REST API without following the JSON:API specification. If implementing a standard provides benefits highly depends on the specific use case.
Upvotes: 2