Home > OS >  Best practice for pagination in API REST
Best practice for pagination in API REST

Time:04-29

I'm new in API developement and I wanted to know what is the best choice to create pagination :

  • GET resquest with query params (sort, limit, etc)
  • POST request with params in the body (sort, limit, etc)

I was more on the GET but my coworkers thinks POST is a better choice, so I just wanted your opinion.

CodePudding user response:

GET would be the usual choice.

General purpose components will understand that the semantics of GET are safe, which means they are also idempotent. If a GET request receives no response, you can automatically retry it without any concerns about loss of property.

Furthermore, if all of the information you need to identify the resource is included in the URI, then you can bookmark the URI, or paste it into an email, or link to it in a document, and it will all "just work".

Also, using GET -- with all of the relevant details encoded into the resource identifier -- means that the response can be cached and re-used. The constraints on caching POST requests mean that you can't capture the information in the request body.


At some point in the future, HTTPWG will register a new HTTP method to cover the safe method with a body case, which may change some of the answers.

In the meantime, it is okay to use GET.

CodePudding user response:

GET is the recommended way to do this, because the answer can be cached and the goal is reading not writing. You can use the query string or range headers for pagination. The exact solution depends on your needs. There are a few standard query languages for this, like OData, but they are overkill for a simple API. Building a custom solution on top of URI templates might be a better choice, or there are non-standard query languages too like RQL, which can be completely or partially implemented in your solution.

  • Related