简体   繁体   中英

REST API HTTP methods

We are planning to use GET for all retrievals and DELETE for all delete operations. HTTP PUT , POST and PATCH is confusing. After a lot of reading, this is my understanding. Please correct if I am wrong.

POST - Not Idempotent ; so can be used for creating new resources/subordinate resource. Each time it creates a new one the ID gets changed and so it is best suited.

PUT - Idempotent; cannot be used for create since the second time the same request comes, it creates a new resource again with different ID. Can be used for update but all attributes should be passed each time it is updated. To achieve this, a GET should be done prior to update operation. Overhead.

Why not use POST for updates too?

PATCH -Not sure if it is suitable for JAX-RS 1.1.

Thanks in advance.

I think your question is this:

Why not use POST for updates too?

The benefit of having POST and PUT make different types of changes to the resource you're allowing an extra vector of the request to modify the action without having to add more URLs. That way, URLs can stay short, clean, and reflective of what it is the represent.

However, to clear things up PATCH is often used for partial updates. So, like PUT it is idempotent, but will only modify the parts of the resource which you send in the request body whereas PUT is expected to modify the whole thing. This is often overlooked in REST. You can read more on the Rest Cookbook website .

Apart from that, from what I can tell your understanding is very good. Enjoy RESTing!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM