REST status code 204 on paginated result

前端 未结 4 726
灰色年华
灰色年华 2021-01-19 05:28

I am designing a REST like API for paginated data retrieval of a YUI-based client. The REST URL looks like this for a GET request:

/app/catalog/data?startInd         


        
相关标签:
4条回答
  • 2021-01-19 05:50

    I would say 204 is most appropriate. The request was successful, just with no results.

    10.2.5 204 No Content
    
    The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation.
    

    Sounds exactly like the case.

    0 讨论(0)
  • 2021-01-19 05:50

    What format do you normally return your results in? I'd be inclined to make this 204 or even 200 returning an empty list.

    0 讨论(0)
  • 2021-01-19 06:04

    I can't decide whether this is 204 or 404.

    Neither. Just return 200 with an empty result (empty XML document or JSON array, whatever you use). Typically I use REST services with paginated views so along with results page I return total number of records. This will help clients to realize the mistake. But technically it is nothing wrong.

    Use 204 for DELETE operations (there's really no content to return) and for PUT.

    BTW (bold mine):

    if no parameters are given, all data from DB will be dumped

    Believe, you don't want to do this...

    0 讨论(0)
  • 2021-01-19 06:08

    HTTP 204 in pagination response is not factible because of the payload. When paginating you should return pagination information like, items returned, total items, offset, etc. which is not allowed with HTTP 204.

    I would use one of:

    • HTTP 200 with count=0 and no items in the returned list.
    • HTTP 400 because invoker asked for an url that is invalid
    • HTTP 404 because items to return are not found

    Choose the one that best suits the way your API works. I think it's safe to return an error (4xx + error info) in this situation because the offset can be exceeded by one of these assumptions:

    • A coding error
    • Invoker is not fetching data from begining (no fresh state)
    • Invoker ignored pagination data (present on each response)
    0 讨论(0)
提交回复
热议问题