Now each request for the same URL may give you a different representation. Does it violet any constraint of REST?
Not inherently. It depends on how the request are different.
- If they are different in time (as latest news suggests), then that's perfectly fine.
- If they are launched by different users who each get personalized content, that's fine too.
- If they are different based on implicitly knowing this user's past actions, that is not okay, as this means you're tracking/relying on a user state and thus are not working stateless.
In order to serve latest news, a server must do some tracking which is the internal state of the server.
The "state" in "stateless" is referring to a user state. I'm not quite sure what you mean by server state, but if you mean things like checking the database (or a third party) for the latest news, that's obviously allowable.
For example, let's say your user loads your page, which calls
/latest_news/ (no parameters), and thus receives articles
A,B,C. While he is reading those articles,
E are added to the server.
A second user calls
/latest_news/ (no parameters) and receives
At a later point in time, the first user calls
/latest_news/ (no parameters) again, and you intend to only give him
D,E because he has already seen
The last part in italics is the issue. You're relying on knowing which articles the user has already read, which means you're tracking their state.
How can I represent something like “latest news” in REST api?
Simply put, your return values cannot be decided based on things you've secretly tracked about the user (implicit parameters).
What would be acceptable, however, is using explicit parameters. For example,
/latest_news/?since=C (which retrieves all articles uploaded after C) is perfectly acceptable, as the server is giving the client exactly what they explicitly ask for.