I have a REST server and a client application running on a mobile device. The client has some data and would like to get updates to the data from the server. How do I do this in a RESTful way in a single transaction?
Suppose the client has the following items:
widget id=1 timestamp=2010-03-05T08:00:00
开发者_如何学Cdoodad id=1 widget=1 timestamp=2010-03-05T08:10
doodad id=2 widget=1 timestamp=2010-03-05T08:20
widget id=2 timestamp=2010-03-05T09:00:00
doodad id=3 widget=2 timestamp=2010-03-05T08:10
and the server has
widget id=1 timestamp=2010-03-05T08:00:00
doodad id=1 widget=1 timestamp=2010-03-05T08:10
doodad id=2 widget=1 timestamp=2010-03-05T09:00
doodad id=4 widget=1 timestamp=2010-03-05T08:30
widget id=2 timestamp=2010-03-05T10:00:00
doodad id=3 widget=2 timestamp=2010-03-05T09:10
doodad id=5 widget=2 timestamp=2010-03-05T08:30
The server should return something like
doodad id=2 widget=1 timestamp=2010-03-05T09:00
doodad id=4 widget=1 timestamp=2010-03-05T08:30
widget id=2 timestamp=2010-03-05T10:00:00
doodad id=3 widget=2 timestamp=2010-03-05T09:10
doodad id=5 widget=2 timestamp=2010-03-05T08:30
containing only the changed rows.
The client would like to tell the server what it has, so that the server can send updates. I do not want to do multiple connections to the server because the mobile connection is unreliable and I do not want to risk getting partial data.
If I were going to do multiple connections, I could probably have the client generate GET requests for each item that it has with the id and timestamp and the server could reply with nothing (304 Not Modified) or the updated values as appropriate. E.g.: "GET /widgets/{id};timestamp=xxx".
What is the best way to do the same thing in a single transaction? Could I do something like "GET /changes" and the body have the type (widget or doodad), id and timestamp values for each row that the client has already? I think I know how to return the values (as a collection of changes), but how does the client ask for them?
Thanks, Ralph
lookup on how to use etags, this kind of thing is what they are for. for small amounts of data it will just be faster to send the entire payload state rather than making multiple calls to the server for each resource.
I like the 3rd answer on this question: Patterns for handling batch operations in REST web services?
精彩评论