I have a restriction in my API where the maximum permitted number of items per page is 50.
What is the correct HTTP code to return to consumer if he put 51, for example?
I thought about HTTP 400 (Bad request), because the consumer "knows" (based in API conventions) that the maximum is 50. In this case I will also return a response with the error described.
The same question for pagination. If I have 20 rows/objects and the API consumer put 21 in the offset param, should I return HTTP 200 with total = 0?
This definitely falls into the 4XX categories of HTTP response.
I think the post appropriate status for this use case is 400 BAD REQUEST
, based on the HTTP spec (https://www.rfc-editor.org/rfc/rfc7231#section-6.5.1)
6.5.1. 400 Bad Request
The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
The user "knows" that API will not accept > 50
and the server will/would try to fulfil the another request that conforms with the rules of your API.
I'm not sure if understood that correctly.
200 OK
would be correct if your API shows all 20 rows/objects instead of silently saying that the request succeed and there's no objects at all!
However:
You might consider 404 Not Found
depending on your business rules if the page 21 does not exist.
From RFC7231 :
The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Maybe you could explain more the second scenario and I'll edit the response accordingly :)
Let's suppose you are working with offset and limit in the request and count (and perhaps total) in the response. You ask for a maximum number of results (limit) skipping n results from the beginning (offset) and in the response you get the items, and the number of items returned (to facilitate automation).
The first scenario should not be an error. It is the same case as the last page (they ask for 50 but you only return remaining).
The second scenario could return one of these:
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With