Best way to create pagination endpoint in RESTful web service

I have an endpoint /entities

on my RESTful web service that returns all persisted objects in the database if called with a request GET

.

What I would like to create now is the pagination functionality. The ability to fetch only the page of these results, not all objects, just to minimize the size of the response.

I am thinking of two ways to do this.

  • Send pagination information using query parameters on the endpoint /entities

    with the query GET

    . For example,/entities?page=1&size=10

  • Use another HTTP Method

    one for example OPTIONS

    (I know it is not meant to be used for this kind of thing). I am not handling requests OPTIONS

    on my web service, and I can take advantage of this while still keeping the essence of a RESTful web service, i.e. using different HTTP Methods

    for different actions. In this case, the endpoint might be something like this /entities/1/10

    , which is (I think) more user-friendly .

Both alternatives can be implemented and I would like to know in advance which one would be more consistent with the REST design standard.

+3


source to share


3 answers


As mentioned above, there is no official standard. But looking at best practices I came across this site:

http://www.restapitutorial.com

There is a .pdf download link on the resources page that contains the full REST guidelines as suggested by the author. Which has, among other things, a section on pagination.

The author suggests adding support for both using the Range header and using query string parameters.

Request

Example HTTP header:

Range: items=0-24

      

Example of query string parameters:



GET http://api.example.com/resources?offset=0&limit=25

      

Where offset is the starting position number and limit is the maximum number of items to return.

answer

The response must contain a Content-Range header indicating how many items are returned and how many items are still not found.

Examples of HTTP headers:

Content-Range: items 0-24/66

Content-Range: items 40-65/*

      

There are several other suggestions in the .pdf for more specific cases.

+3


source


I believe there is no official "standard" for RESTful web services. But there are many recommendations / implementations from different vendors.

Your first way, using query parameters, will be correct. Different HTTP methods (GET, PUT, DELETE, POST) are used for different kinds of operations. I've never heard of them being used to modify existing operations.



See Wikipedia - REST

0


source


Based on the HATEOAS (hypermedia as an engine of application state) standard, the answer should be linked to the previous and next pages.

/entities?page=1&size=10

rather than /entities/1/10

1/10 because (strictly speaking) 1/10 is not a resource that must be located via a URI.

0


source







All Articles