Thursday, June 5, 2014

API & RESTFUL

Which HTTP methods match up to which CRUD methods?

Why it changed from PUT to PATCH https://github.com/rails/rails/issues/348

http://stackoverflow.com/questions/6203231/which-http-methods-match-up-to-which-crud-methods

GET = READ
POST = WRITE

Create = PUT with a new URI
         POST to a base URI returning a newly created URI
Read   = GET
Update = PUT with an existing URI
Delete = DELETE
PUT can map to both Create and Update depending on the existence of the URI used with the PUT.
POST maps to Create.
Correction: POST can also map to Update although it's typically used for Create. POST can also be a partial update so we don't need the proposed PATCH method.


http://stackoverflow.com/questions/19504434/rails-put-vs-post

According to rails convention,

PUT is used for updating an existing resource

POST is used for creating a new resource

In rails 4, PUT has been changed to PATCH to avoid confusion.

Rails generated routes will look like below by default

    posts GET    /posts(.:format)                            {:action=>"index", :controller=>"posts"}
          POST   /posts(.:format)                            {:action=>"create", :controller=>"posts"}
 new_post GET    /posts/new(.:format)                        {:action=>"new", :controller=>"posts"}
edit_post GET    /posts/:id/edit(.:format)                   {:action=>"edit", :controller=>"posts"}
     post GET    /posts/:id(.:format)                        {:action=>"show", :controller=>"posts"}
          PUT    /posts/:id(.:format)                        {:action=>"update", :controller=>"posts"}
          DELETE /posts/:id(.:format)                        {:action=>"destroy", :controller=>"posts"}
Notice the action for PUT and POST

The request methods of the HyperText Transfer Protocol (HTTP) computer protocol are a common example of idempotence, in that data retrieval operations can be performed without changing or otherwise affecting the data.

No comments:

Post a Comment