Resource Routes - PUT vs PATCH |
Assuming that PATCH makes it possible to update one attribute at a time while the PUT forces the update of the entire entity.
Is not more versatile the default Update route for Resource be a PATCH method? Why?
(07-21-2017, 01:46 AM)Narf Wrote:(07-20-2017, 06:15 PM)natanfelles Wrote: Assuming that PATCH makes it possible to update one attribute at a time No. Yes, with PATCH you can update as many attributes as you need individually. But, as I understand it, the PUT is used to replace the entire entity. I believe that PATCH makes the application more flexible because it does not "force" the client to send all the attributes to be updated, but only what is necessary. I saw in most of the documentation recommending the use of PUT as the default for updating, but studying the difference between PUT and PATCH I had this doubt. Here are some opinions and examples to take into consideration: https://stackoverflow.com/questions/2845...e-examples (07-21-2017, 09:37 AM)natanfelles Wrote: I believe that PATCH makes the application more flexible... Flexible != Rigid REST API. If you want flexible, GET and POST all the way home. https://www.youtube.com/watch?v=WO23WBji_Z0
(07-21-2017, 04:37 PM)skunkbad Wrote:(07-21-2017, 09:37 AM)natanfelles Wrote: I believe that PATCH makes the application more flexible... Ok. Then is used PUT by default to the system be rigid. Now I think that I understand why this is the default verb in the Resource Routes. ![]()
I guess my knowledge was simply based on old practices. Reading back through RFC7231 and RFC5789 it appears you are correct.
Quote:The difference between the PUT and PATCH requests is reflected in the To the best of my knowledge those are the two most current versions of the relevant RFCs. So, yes, PATCH should be the default. In the end, it doesn't really matter because it's all in how you code your application to behave, anyway ![]()
It is my understanding that PATCH, while "more correct", is not used instead of POST because of the ACTION attribute of an HTML form. The only way to generate a PATCH would be through an AJAX request
(07-22-2017, 09:11 PM)ciadmin Wrote: It is my understanding that PATCH, while "more correct", is not used instead of POST because of the ACTION attribute of an HTML form. The only way to generate a PATCH would be through an AJAX request Yes. But I'm referring to Resource Routes. (07-22-2017, 09:21 PM)natanfelles Wrote:(07-22-2017, 09:11 PM)ciadmin Wrote: It is my understanding that PATCH, while "more correct", is not used instead of POST because of the ACTION attribute of an HTML form. The only way to generate a PATCH would be through an AJAX request Good point ![]()
Explaning more...
If you have a "product" entity with the following attributes: - id - title - short_description - description - price - stock - uri - rate - bla Every time that you need to update it. The PUT "forces" you to send all the attributes because have the function of REPLACE the entire entity. Then, if you need update ONLY the price attribute, you need to send ALL the other attributes. The PATCH method enables you to send ONLY the price attribute. It is more flexible, save bandwidth traffic data and processing. I read the GitHub REST API docs and see that it uses PATCH. I did some research and thought, "Why is PUT the default update verb in CI?"... And here is the question... This change is only in the HTTP verb of Update. With a HTML form request nothing changes, continues to be with POST. * I thought, originally, in Portuguese, Hahah. |
Welcome Guest, Not a member yet? Register Sign In |