API pagination best practices

前端 未结 11 1235
执念已碎
执念已碎 2020-11-28 17:12

I\'d love some some help handling a strange edge case with a paginated API I\'m building.

Like many APIs, this one paginates large results. If you query /foos, you\'

11条回答
  •  我在风中等你
    2020-11-28 17:34

    You have several problems.

    First, you have the example that you cited.

    You also have a similar problem if rows are inserted, but in this case the user get duplicate data (arguably easier to manage than missing data, but still an issue).

    If you are not snapshotting the original data set, then this is just a fact of life.

    You can have the user make an explicit snapshot:

    POST /createquery
    filter.firstName=Bob&filter.lastName=Eubanks
    

    Which results:

    HTTP/1.1 301 Here's your query
    Location: http://www.example.org/query/12345
    

    Then you can page that all day long, since it's now static. This can be reasonably light weight, since you can just capture the actual document keys rather than the entire rows.

    If the use case is simply that your users want (and need) all of the data, then you can simply give it to them:

    GET /query/12345?all=true
    

    and just send the whole kit.

提交回复
热议问题