GET /api/v1/departments/15
In above example API returns department with ID=15 from a collection called departments
Basic rules:
- You must use HTTP GET method for finding a resource.
- End point may be plural followed by identifier or it can be singular too
- Proper http response codes must be returned
- Do not use body, use uri parameters instead
Http response codes to return
HTTP Code | When to return |
---|---|
200 - OK | When resource exists. Also, return a response with expected information |
404 - Not found | When resource does not exist. |
Additionally -
You may also use query parameters to select fields which you want to return in response. This helps when you want less fields on presentation layer which ultimately reduces unwanted network traffic. Asp.net Web API 2 provides OData $select to select specific fields e.g.
GET /api/v1/departments/15?$select=Name
Above API can return only Name of the department.
No comments:
Post a Comment