Is returning a subset of the representation in a POST response a violation of REST?
If I am POSTing a new resource with 100 fields, and the server adds 3 fields of its own, like date created, status, etc., is it RESTful to only return a mini-representation of the resource that includes only the 3 new fields in the body of the 201 CREATED response?
Then the client can just add those 3 new fields to its local representation. I have seen exhortations that one should al开发者_开发技巧ways send the full representation, but it seems wasteful of bandwidth to return all 103.
The 201 response entity (the body of the response itself) doesn't have to be, or is considered by any http client as, the resource you just created.
It's a representation that describes the result.
If you want people to access the resource that was just created, they can do so by issuing a request to the URI in the Location header that comes back with a 201.
If you return an entity body in your 201, it is not considered by HTTP as being the resource you just created, so you can return whatever you want.
The important thing is the media type of the entity you return. If that entity is known by the client, be it that it's a smaller or the full version of the entity, they'll know what to do with it. If you expect the clietn to "know" that the returned media type to a 201 is a minimized version, you're enforcing strong coupling to your own protocol, which is in breach of ReST principles.
There are no rules that claim you need to return the full response in the REST dissertation or the HTTP RFC. The latter (RFC 2616, HTTPbis version) has this to say about the 201 Created status code:
If a resource has been created on the origin server, the response SHOULD be 201 (Created) and contain an entity which describes the status of the request and refers to the new resource, and a Location header.
Returning the information that has been added is a reasonable and perfectly RESTful HTTP-compliant idea.
It's entirely up to you what you respond with though it might be an idea to provide a link to more information about the resource. If you want to get really fancy you can specify a microformat
精彩评论