Tuesday, October 8, 2013

Clone a resource in HTTP

In order to clone or copy a file, we execute a command like
cp a b
Given a file named a, the command creates a new file named b that has the same content as a. Similarly, we can clone a resource A. The client sends the following request to the server.
PUT /resources/B
...
{"source": "/resources/A"}
However, a client normally does not have the knowledge to properly name a new resource. A more reasonable alternative is the following request.
POST /resources
...
{"source": "/resources/A"}
All these are straightforward, but really smell like RPC. They did not follow the REST constraint of self-descriptive message. How could the server figure out /resources/A is what the client wants. What if the resource has been changed or deleted after the request was sent out?  The following approach may be better:
GET /resources/A
Then
POST /resources
...
the representation of /resources/A
What if the representation is huge? Maybe we can do the following:
GET /resources/A
or
HEAD /resources/A
Then
POST /resources
...
{"source": "/resources/A",
"ETag": "frompreviousrequest"}

No comments:

Post a Comment