We are building a restful service for serving employee data.
We have an api which will return the list of employees who belong to a specific department of a client.
This api takes 2 parameters - clientId and departmentId
As per standards which of the below is the right way to construct a restful url for this api?
1) /client/{clientId}/department/{departmentId}/employees
2) /client/{clientId}/employees?departmentId={departmentId}
Can a restful url can have multiple path parameters? If yes/no to above question - why it is so?
Query parameters are passed after the URL string by appending a question mark followed by the parameter name , then equal to (“=”) sign and then the parameter value. Multiple parameters are separated by “&” symbol.
An endpoint can have multiple path parameters, like in the example `/organizations/{orgId}/members/{memberId}`. This would be pointing to a specific member's record within a specific organization, with both `{orgID}` and `{memberID}` requiring variables.
URI parameter (Path Param) is basically used to identify a specific resource or resources whereas Query Parameter is used to sort/filter those resources. Let's consider an example where you want identify the employee on the basis of employeeID, and in that case, you will be using the URI param.
In RESTful APIs the path parameters are used to identify a resource (client, order, blog post etc). This resource is often a record in some database. Some database tables have composite keys e.g if you have a system that stores data about the employees of several different clients then there might be entries in your database like
name | client_id | department_id
John | 1 | 1
Jane | 2 | 1
Where both clients have a department with id 1
.
In that case if the purpose is to identify the resource of list of all employees in a given department for a given client then it makes sense to use several path parameters.
/client/{clientId}/department/{departmentId}/employees
However if this is more of a search API then it might make sense to have
employees?age={age}&height={height}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With