tags:

views:

180

answers:

3

I know REST is meant to be resource-oriented, which roughly translates to CRUD operations on these resources using standard HTTP methods. But what I just wanted to update a part of a resource?

For example, let's say I have Payment resource and I wanted to mark its status as "paid". I don't want to POST the whole Payment object through HTTP (sometimes I don't even have all the data).

What would be the RESTful way of doing this? I've seen that Twitter uses the following approach for updating Twitter statuses:

http://api.twitter.com/1/statuses/update.xml?status=playing with cURL and the Twitter API

Is this approach in "the spirit" of REST?

UPDATE: PUT -> POST

Some links I found in the meantime:

A: 

I guess, that's what POST is for: The 'U' in 'CRUD'.

You POST data to an existing resource. The resource decides what to do with it, and gets updated. Also, the POST data may be only a fragment of the complete resource.

Twitter's approach is IMHO not RESTful, because they overload GET.

Boldewyn
Boldewyn, I thought PUT is for update. Looks like there's confusion about this: Wikipedia says one thing (http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services), and there are other voices (http://16cards.com/2007/03/10/the-cafes-put-is-not-update/).Fragments: this means the XSD must not enforce any mandatory stuff (except for primary keys)?
Igor Brejc
PUT and POST mean exactly what is written in the HTTP spec. No more, no less. PUT means update in the sense of 'replace current state of resource with this state' and POST just means 'process this (according to your nature)'. You *can* do partial updates with POST on a sub-resource (e.g. POST /payments/2/post-here) but you need to define all the payload types. The PUT with 303 approach is simpler.
Jan Algermissen
Twitter's API is not RESTful because it does not make use of media type semantics but provides a static description of the available resources and operations. That is not REST (which does not mean that it is not useful).The update seems to require a POST, so at least I do not see twitter using a side effect free method (GET) for change operations.
Jan Algermissen
+1  A: 

POST should be used for modifying a resource

EDIT: Martin Fowler's article Richardson Maturity Model is a very good intro to REST.

Dominik
@Dominik, actually it's a bit more complicated than that. See the links I've added in the question.
Igor Brejc
+3  A: 

Igor,

the ideal way of doing this is to change a part (sub resource) of the resource and have the server return a 303 See Other with Location header to point to the altered resource. The 303 See Other tells the client that as the result of the request some other resource has changed and that the client should update the representation it holds.

In your example (media types hypothetical, of course):

1. Client retrieves payment representation
GET /payments/2

200 Ok
Content-Type: application/payment+xml

<payment>
  <status href="/payments/2/status" value="pending"/>
</payment>

2. Client updates status
PUT /payments/2/status
Content-Type: text/plain

payed

303 See Other
Location: /payments/2

3. Client follows the 303 redirect 
GET /payments/2

200 Ok
Content-Type: application/payment+xml

<payment>
  <status href="/payments/2/status" value="payed"/>
</payment>

Jan

Jan Algermissen
@Jan, yes, now that I've delved more into REST PUT/POST articles, I think I understand the concept.
Igor Brejc