Querying resources
You can use a set of common parameters to customize the results returned from a GET operation, such as sorting or filtering. Each REST API specification lists the set of available common parameters. For more information, see Common REST API Parameters.
Pagination when querying for a collection of resources
When you perform a GET operation to retrieve multiple resources (that is, a GET on a collection URI, such as /rest/server-profiles), the resources are returned in a collection object that includes an array of resources along with information about the set of resources returned. This collection of resources may be automatically truncated into pages to improve performance when a query would return a large number of resources. The collection attributes (described below) provide information needed to determine whether the full set of resources were returned, or if additional queries are required to retrieve additional pages.
For example, a collection object includes a next page and previous page URI. These URIs indicate whether additional pages are available, and can be retrieved via a GET operation on these URIs. This provides a simple model for ensuring all resources in the query have been retrieved, by doing iterative GETs on the nextPageUri attribute until the attribute comes back empty/null (See Example: Return all resources in a specific collection query below.).
It’s also possible to query for a specific page of resources, using the start and count query parameters. These parameters indicate the index of the first resource to be returned, and the number of resources to return in the page, respectively.
Attributes returned in all GET operations performed on a collection URI, for example/rest/server-profile:
|
|
The total number of resources available in the requested collection (taking into account including any filters). Not necessarily what was returned. |
||||||
|
|
The actual number of resources returned (in the members attribute). |
||||||
|
|
The zero-based index of the first item returned (in the members attribute). |
||||||
|
|
The array of resources returned in the current result set. |
||||||
|
|
A URI that can be used to query for the next page in the result set (using the same |
||||||
|
|
A URI that can be used to query for the previous page in the result set (using the same
|
Example: Return all resources in a specific collection query
The number of resources returned in a query might not match what was specified in the count parameter. Clients must always check the returned results to determine whether the full results set was returned or not. The two reasons that all the resources may not be returned in a query are:
-
You've reached the last page of the query (and there are simply not that many resource left to return). This is also indicated by a returned
prevPageUriwith anullvalue. -
For performance reasons, the service may automatically truncate the returned result set, requiring additional
GETrequests (with appropriate start & count parameters set) in order to retrieve the full set of resources.
The simplest way to make sure that you have retrieved all resources in a specific collection is to always perform iterative GET requests using the returned nextPageUri until the value is null. See the following example in pseudo-code based on any filters/queries and sort order:
currentCollection = doGet("/rest/server-hardware");
allResources = currentCollection.members;
While (currentCollection.nextPageUri) {
currentCollection = doGet(currentCollection.nextPageUri);
allResources.Append(currentCollection.members);
}
![[NOTE: ]](images/note.gif)