Alechko
Alechko

Reputation: 1598

Using HTTP status code 206 - Partial Content for pagination

Background

Our team has brought up the idea of using status code 206 - Partial Content in our REST API to indicate that GET requests over large datasets have more content than was returned, in order to allow the API user to paginate. This would be an alternative to the hasMore flag we are currently use in the response body.

Pro

Con

Question

In which cases could or should 206 - Partial Content be used for pagination of results sets sent in response to restful GET requests?

Upvotes: 3

Views: 2790

Answers (1)

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57397

Our team has brought up the idea of using status code 206 - Partial Content in our REST API to indicate that GET requests over large datasets have more content than was returned, in order to allow the API user to paginate

That sounds like a bad idea.

IANA HTTP status code registry currently identifies RFC 9110 as the authoritative definition of the 206 Partial Content status code.

The 206 (Partial Content) status code indicates that the server is successfully fulfilling a range request....

If you aren't already familiar with range requests, review RFC 9110, section 14. But short version: if the incoming request doesn't include a Range header, then you don't have a range request.

Trying to repurpose 206, which has a specific meaning in the general purpose transfer of documents over a network domain, to signal some meaning that is local to your bespoke resource model, increases the possibility that general purpose components misunderstand what is going on; the benefits you hope to realize are unlikely to outweigh the risks.

TL;DR: be normal - return a response with a 200 status code when that's what a boring web server would do.


If you are trying to indicate paging; status code is the wrong tool. Look instead toward Web Linking using link relations like first/last/next/prev.

Upvotes: 7

Related Questions