Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rest Standard: Path parameters or Request parameters

Tags:

rest

I am creating a new REST service.

What is the standard for passing parameters to REST services. From different REST implementations in Java, you can configure parameters as part of the path or as request parameters. For example,

Path parameters http://www.rest.services.com/item/b

Request parameters http://www.rest.services.com/get?item=b

Does anyone know what the advantages/disadvantages for each method of passing parameters. It seems that passing the parameters as part of the path seems to coincide better with the notion of the REST protocol. That is, a single location signifies a unique response, correct?

like image 607
onejigtwojig Avatar asked Jul 07 '10 20:07

onejigtwojig


People also ask

What is the difference between path and query parameters when should you use them?

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.

What is request parameter in rest API?

API Parameters are options that can be passed with the endpoint to influence the response. In GET requests, they're found in strings at the end of the API URL path. In POST requests, they're found in the POST body.

How do I use path parameters in rest API?

Path parameters are used to identify a resource uniquely. In the {server_host}/students/{student_id} example, student_id is identifying a unique student_id . If we remove student_id from the path parameter, create {server_host}/students API and use student_id of the request body.

What are path parameters in API?

Path parameters are variable parts of a URL path. They are typically used to point to a specific resource within a collection, such as a user identified by ID. A URL can have several path parameters, each denoted with curly braces { } . GET /users/{id}


2 Answers

Paths tend to be cached, parameters tend to not be, as a general rule.

So...

GET /customers/bob 

vs

GET /customers?name=bob 

The first is more likely to be cached (assuming proper headers, etc.) whereas the latter is likely not to be cached.

like image 63
Will Hartung Avatar answered Sep 21 '22 17:09

Will Hartung


tl;dr: You might want both.


Item #42 exists:

GET /items/42 Accept: application/vnd.foo.item+json --> 200 OK {     "id": 42,     "bar": "baz" }  GET /items?id=42 Accept: application/vnd.foo.item-list+json --> 200 OK [     {         "id": 42,         "bar": "baz"     } ] 

Item #99 doesn't exist:

GET /items/99 Accept: application/vnd.foo.item+json --> 404 Not Found  GET /items?id=99 Accept: application/vnd.foo.item-list+json --> 200 OK [ ] 

Explanations & comments

  1. /items/{id} returns an item while /items?id={id} returns an item-list.
  2. Even if there is only a single element in a filtered item-list, a list of a single element is still returned for consistency (as opposed to the element itself).
  3. It just so happens that id is a unique property. If we were to filter on other properties, this would still work in exactly the same way.
  4. Elements of a collection resource can only be named using unique properties (e.g. keys as a subresource of the collection) for obvious reasons (they're normal resources and URIs uniquely identify resources).
  5. If the element is not found when using a filter, the response is still OK and still contains a list (albeit empty). Just because we're requesting a filtered list containing an item that doesn't exist doesn't mean the list itself doesn't exist.

Because they're so different and independently useful, you might want both. The client will want to differentiate between all cases (e.g. whether the list is empty or the list itself doesn't exist, in which case you should return a 404 for /items?...).

Disclaimer: This approach is by no means "standard". It makes so much sense to me though that I felt like sharing.

PS: Naming the item collection "get" is a code smell; prefer "items" or similar.

like image 29
tne Avatar answered Sep 19 '22 17:09

tne