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?
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.
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.
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.
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}
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.
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 [ ]
/items/{id}
returns an item
while /items?id={id}
returns an item-list
.item-list
, a list of a single element is still returned for consistency (as opposed to the element itself).id
is a unique property. If we were to filter on other properties, this would still work in exactly the same way.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.
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