Domanda

I'm facing a problem right now.

We and our team are developing a new app with REST. The problem we are facing is we need sometimes get some kind of data and don't know how to create the URI.

for example. If i need a list of products then api/products/ (GET) will do the trick. A single product, api/products/1 (GET)

what if i just need a list of products out of stock, or a products with just 1 item left?

what URI would I user?. I don't think REST can do it.

Thanks you.

BTW: I'm using VS 2012 Web API.

È stato utile?

Soluzione

You can use "query parameters" for "filtering" in REST, it is a common approach. Keep in mind that REST is a "architectural style", i.e., a set of principles that leave some room for interpretation. In your case I would say you could create something like this:

  • query all products in stock: GET /api/products?instock=true
  • query products with only 1 left: GET /api/products?countinstock=1

Where "instock" and "countinstock" are "query parameters", which you use in your REST API implementation as filters to query you DB.

This approach is commonly accepted in RESTful designs, namely you still have the "resource" (products) on which you perform a GET action/verb, the only extra part is the "query parameters" used to filter and constraint the "action" upon the "resources. See some further discussions in this topic (and supporting the user of "query parameters") here:

Altri suggerimenti

Why not just modify your controller to take multiple parameters like id and inStock? For instance your URI could be something like api/products?id=1&inStock=0

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top