I'm developing a REST api, and I'm wondering the following:
I want to use HTTP PUT to update some entities in the webservice. The format will be a urlencoded body. Is it acceptable to only update the fields that were actually specified, rather than the entire entity?
I'm asking, because PUT would be a very convenient method to do some updates, but I don't want them to remove fields if they just happened to misspell some of them. Additionally, I don't want to force the implementor to always have to do a GET first and copy every single field they might not actually use..
In short, the PUT method is used to create or overwrite a resource at a particular URL that is known by the client.
If the target resource does not have a current representation and the PUT successfully creates one, then the origin server MUST inform the user agent by sending a 201 (Created) response.
Partial updates are allowed by PUT (according to RFC 7231 https://www.rfc-editor.org/rfc/rfc7231#section-4.3.4). ",... PUT request is defined as replacing the state of the target resource." - replacing part of object basically change state of it.
The HTTP PUT request method creates a new resource or replaces a representation of the target resource with the request payload.
You could simply POST the updated properties to the resource. Remember POST is the catch-all verb that you can use to do whatever you need to do when the other verbs don't work for you.
Check out Roy's article It's ok to use POST
Put is only for complete replacement. There is a proposal for the verb PATCH to address the problem you have (http://www.ietf.org/internet-drafts/draft-dusseault-http-patch-14.txt)
Patch, however, still may not be what you want. What is sent is an update resource that can do things like increment counters and so, unlike put, isn't idempotent.
You could expose each field as a resource and do multiple puts to each field. You can pipeline the puts to mitigate the extra latency.
I would say it may make sense. I consider the REST idea very flexible, so if you Update one entity why not only transfer the fields that need to be updated in your implementation. It is true, that it needs more server sided effort though. You have to check if the entity is available and can be updated with the data transferred and you need validation checks (as opposed to schema free document oriented data).
<!-- PUT books/1337 -->
<book>
<title>Hello</title>
<author>John Doe</author>
</book>
<!-- PUT books/1337 -->
<book>
<title>Hello here I am</title>
</book>
I've never liked any of the solutions for partial updates, either. If I were designing a web service for widespread use, I'd probably go with POST
. If it were for use by a fairly small number of people, i.e., I could talk to all the people I expected to call it, I've had two different ideas for addressing it.
PUT
to a new 'update' resource. It would basically record the update you want to apply and then be in charge of not applying duplicates. I envision this working somewhat like a version control system that keeps a list of patches/changesets and gets pretty complicated every time I try to think out all the corner cases.
PUT
to the resource, but don't change any fields that are not present. Require fields that you want to NULL
out to be present with a special attribute indicating that you want to NULL
it out. This seems far more practical, but doesn't fit in very well with the consensus that PUT
should be a complete update.
If anyone has pointers to discussions of similar ideas, please edit/comment accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With