Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTful url to GET resource by different fields

Tags:

rest

Simple question I'm having trouble finding an answer to..

If I have a REST web service, and my design is not using url parameters, how can I specify two different keys to return the same resource by?

Example I want (and have already implemented)

/Person/{ID}

which returns a person as expected.

Now I also want

/Person/{Name}

which returns a person by name.

Is this the correct RESTful format? Or is it something like:

/Person/Name/{Name}
like image 926
Erix Avatar asked Jun 19 '12 15:06

Erix


People also ask

Which URL pattern is recommended when working with one resources and a collection of resources?

Relative vs Absolute. It is strongly recommended that the URLs generated by the API should be absolute URLs. The reason is mostly for ease of use by the client, so that it never has to find out the correct base URI for a resource, which is needed to interpret a relative URL.

Which RESTful method is used to retrieve resources from server?

Simply put, the GET method is used to retreive data from a server at the specified resource.

How do I use REST API URL?

REST API URL in Hub For example, you have your company's server www.mycompany.com and a Hub service. You can configure Hub to be accessible by www.mycompany.com/hub or, let's say hub.mycompany.com . /api/rest/ is the context path for the REST API of your Hub service.

What is recommended method and URL pattern for retrieving a specific user?

What is the recommended method and URL pattern for retrieving a specific user? GET /user/{id} GET /users/{id}


2 Answers

You should only use one URI to refer to a single resource. Having multiple URIs will only cause confusion. In your example, confusion would arise due to two people having the same name. Which person resource are they referring to then?

That said, you can have multiple URIs refer to a single resource, but for anything other than the "true" URI you should simply redirect the client to the right place using a status code of 301 - Moved Permanently.

Personally, I would never implement a multi-ID scheme or redirection to support it. Pick a single identification scheme and stick with it. The users of your API will thank you.

What you really need to build is a query API, so focus on how you would implement something like a /personFinder resource which could take a name as a parameter and return potentially multiple matching /person/{ID} URIs in the response.

like image 168
Brian Kelly Avatar answered Sep 29 '22 10:09

Brian Kelly


I guess technically you could have both URI's point to the same resource (perhaps with one of them as the canonical resource) but I think you wouldn't want to do this from an implementation perspective. What if there is an overlap between IDs and names?

It sure does seem like a good place to use query parameters, but if you insist on not doing so, perhaps you could do

person/{ID} 

and

personByName/{Name}
like image 39
pc1oad1etter Avatar answered Sep 29 '22 10:09

pc1oad1etter