RESTful url to GET resource by different fields

前端 未结 3 709
刺人心
刺人心 2020-12-19 09:44

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 k

相关标签:
3条回答
  • 2020-12-19 10:20

    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.

    0 讨论(0)
  • 2020-12-19 10:20

    I generally agree with this answer that for clarity and consistency it'd be best to avoid multiple ids pointing to the same entity.

    Sometimes however, such a situation arises naturally. An example I work with is Polish companies, which can be identified by their tax id ('NIP' number) or by their national business registry id ('KRS' number).

    In such case, I think one should first add the secondary id as a criterion to the search endpoint. Thus users will be able to "translate" between secondary id and primary id.

    However, if users still keep insisting on being able to retrieve an entity directly by the secondary id (as we experienced), one other possibility is to provide a "secret" URL, not described in the documentation, performing such an operation. This can be given to users who made the effort to ask for it, and the potential ambiguity and confusion is then on them, if they decide to use it, not on everyone reading the documentation.

    In terms of ambiguity and confusion for the API maintainer, I think this can be kept reasonably minimal with a helper function to immediately detect and translate the secondary id to primary id at the beginning of each relevant API endpoint.

    It obviously matters much less than normal what scheme is chosen for the secret URL.

    0 讨论(0)
  • 2020-12-19 10:28

    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}
    
    0 讨论(0)
提交回复
热议问题