Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rest URL Standards - Multiple Path Parameters

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?

like image 237
Kalyan Chakravarthy S Avatar asked Mar 27 '17 06:03

Kalyan Chakravarthy S


People also ask

How do I pass multiple query parameters in REST URL?

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.

Can you have multiple path parameters?

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.

What is path parameter and query parameters in REST API?

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.


1 Answers

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}
like image 83
Simon Avatar answered Nov 15 '22 21:11

Simon