Reputation: 1641
While not entirely RESTful, I think it makes sense to use actions where you're doing something more conceptually involved than just manipulating simple data. Therefore I have adopted the following scheme:
/api/projects/ = Returns all projects
/api/projects/{id} = Returns single project
/api/projects/{id}/{action} [POST]
Applies action on a project, such as "Activate"
/api/projects/{action} [GET]
Gets projects which meet the condition of the action, such
as all projects which are "Active"
But... Providing a way to get Projects for a Client is not so clear, I'm leaning towards option 2...
1. /api/projects/GetByClient [POST, Client Id in body]
But now it has to be a POST, when we're only getting a resultset...
2. /api/clients/{clientid}/GetProjects
But now we're returning projects from a clients controller
3. /api/ProjectGetterForClient/{client}
But will result in a large number of URLs for a complex project
How should I go about making this kind of API available?
If #2 is the correct option. Should I be returning just the list of Projects. Or return the Client, which contains a list of Projects?
Upvotes: 1
Views: 82
Reputation: 1315
I believe the most RESTful way would be:
GET: /api/clients/5/projects
This would return all projects for client with ID 5
.
You could also use:
GET: /api/projects?clientId=5
If your caller is likely to use project data whenever they request a client, then include it in the result. Otherwise you're better off keeping them as separate requests.
Upvotes: 3
Reputation: 18433
Have you heard about OData? It extends basic REST features and provides standardized format of queries.
For instance:
Requesting an Individual Entity by ID
The request below returns an individual entity of type Person by the given UserName “russellwhyte”
GET serviceRoot/People('russellwhyte')
http://www.odata.org/getting-started/basic-tutorial/
Or...
GET serviceRoot/Airports('KSFO')/Location/Address
In your case it could be I think more or less...
GET api/Clients('clientid')/Projects
As user1620220 mentioned - Web API has built-in support for OData: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options
Upvotes: 2