So, I'd like to know how many results I'll be getting back from a RESTful uri GET request. I don't know of any way to do that at this point. Is there a way to do that? Since REST just throws out properties, I don't know if it is able to take a count of its results, but it can skip results and take a subset of results.
Anybody have any suggestions?
Oh my setup is a LINQ to SQL that populates a queriable generic List. The data service makes that list available. I've tried getting a count on the list, but I always get the max rows of the database back, and that isn't what I'm looking for.
Why don't you make your resource handle queries for that type of metadata? Suppose that
returns your list of items like this:
Then something like:
could return an empty list like this:
or possibly a generic info document like this:
You could also implement an "info" resource like this:
which might return:
Other people might have objections to this concept, but, this seems reasonable to me:
And then:
Note: A HEAD request is used here to obtain the count without having to pull the full data set. HEAD requests retrieve only the HTTP headers, not the body of the response.
This would be the most RESTful way I can think of indicating how many results you're gonna get back before you send it over the wire. The main trick is just coming up with the best header name for it.
X-Result-Count
is decent, but if you can find prior art and reuse their header name choice, that would be even better (as long as they didn't name it something really dumb). That said, I don't expect you'll have much luck, so you should probably stick withX-Result-Count
.Also, I think you may have misunderstood what "REST" actually entails. There's no reason you can't give a representation by range. For example:
However, to be RESTful, you need to be able to tell the client about the representation identified by
/your/api?range=0-9
or/your/api?page=1&perpage=10
without using out-of-band information. For example, if your/your/api
page would return too many results, do a temporary redirect to/your/api?page=1&perpage=10
, and include hyperlinks to/your/api?page=2&perpage=10
. Note that a hyperlink in this context could be something simple like:Now the information to navigate the results of your API calls is in-band and actually RESTful.
Essentially, REST is plain-old-HTTP with caching done right and usually sensible URIs thrown in for good measure. It's also "hypertext as the engine of application state" (i.e. resources should link to other resources). It is not a protocol, it's an architectural style. Anyone who tells you differently had better be named Roy Fielding.
Addenda:
If you want to indicate the total count versus the page count, you can define the header like so:
Or adjust as necessary.
Why not have the REST webservice just return the data as a JSON or XML, and in there you can have a property about length.
You should be able to take care of this in your REST resource name design. You'd start with something like:
You might quickly decide that "/widgets" will be a humongous list and decide to support pages, something like
In other cases you can subdivide a large set into a natural hierarchy:
And you can define queries too: