How can I get a REST resource using different search methods?

Let's say you have a resource / company that allowed customers to search for public companies and you wanted customers to be able to search for businesses by ticker, location, location and industry

You would store the same url in the form:

  • GET / companies / msft
  • GET / companies / usa
  • GET / companies / usa & software

It doesn't seem right. Any ideas?

+2


source to share


3 answers


What about?

GET /Companies?ticker=MSFT

GET /Companies?country=USA

GET /Companies?country=USA&industry=software

      

It is important to identify the resource. A resource is a "list of companies". Its media type can be an Atom list or just an HTML document using UL LI tags. Query parameters affect the contents of the list, but conceptually it is still a "list of companies".

You can create a separate resource like

GET /Companies/USA

      



but you really need to. Are you going to travel to / Companies / USA? Are you going to delete / Companies / USA? If your application doesn't require the ability to perform additional operations on these specific sets of companies, then why would they model them as separate resources?

As a side note to this discussion, I would like to differentiate more clearly when I access a resource that is a single entity and a list. i.e.

GET /Companies/USA

GET /Company/MSFT

      

I realize this is not how some of the popular web frameworks work, but I found it a useful distinction.

+9


source


You can accept any of these, but then return a Location: header pointing to the canonical address (presumably GET / company / msft).



0


source


You only have one company, but multiple ways to get to it, so I would probably define / companies / [unique-name] followed by various things like / companies / byticker / msft and / companies / bylocation / usa and etc.

0


source







All Articles