GET /api/v1/departments/15/employees
In above example API returns all employees from department with ID=15Basic rules:
- You must use HTTP GET method for finding a resource.
- End point must be plural
- 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 collection's parent record does not exist. In above example return 404 if department 15 does not exist |
204 - No Contents | When resource collection is empty. In above example return 204 if there are no employees in a department |
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/employees?$select=Name
Above API can return only Names of the employees present in department with id=15.Performance consideration
- Use pagination so that you do not return whole data at once. Pagination helps in fragmenting data, reduce unwanted network traffic, speed up data query.
No comments:
Post a Comment