Paging in a Rest Collection

前端 未结 12 1844
走了就别回头了
走了就别回头了 2020-12-02 03:37

I\'m interested in exposing a direct REST interface to collections of JSON documents (think CouchDB or Persevere). The problem I\'m running into is how to handle the G

12条回答
  •  无人及你
    2020-12-02 04:42

    While its possible to use the Range header for this purpose, I don't think that was the intent. It seems to have been designed for handling flaky connections as well as limiting the data (so the client can request part of the request if something was missing or the size was too large to process). You are hacking pagination into something that is likely used for other purposes at the communication layer. The "proper" way to handle pagination is with the types you return. Rather than returning questions object, you should be returning a new type instead.

    So if questions is like this:

    ...

    The new type could be something like this:

    50 10 1203 ..

    Of course you control your media types, so you can make your "pages" a format that suits your needs. If you make is something generic, you can have a single parser on the client to handle paging the same for all types. I think that is more in the spirit of the HTTP specification, rather than fudging the Range parameter for something else.

提交回复
热议问题