8 Falsehoods Rails developers believe about REST
4. Every successful POST request should respond with a 201
A common misconception is that POST requests are only to be used to create a new resource (or a new database record), and so should always return status 201 on a successful request to indicate that the resource was created successfully.
5. Always return 201 when a request creates a new database record
The HTTP 201 status code is used to indicate that a new resource has been made available by the application. Sometimes resources represent a database record, but sometimes they do not. Sometimes DB records are created, but a new resource is not.
A good heursitc to decide if you should return 201 or 200, is to ask: Has a new URL been made available by this request (e.g. POST /users.json
=> creates => Location: /users/1.json
).
7. PATCH and PUT are synonymous
Rails made the transition from using PUT as the default verb when updating a record, to using PATCH in Rails 3.
This seamless transition gave the impression that these two verbs are interchangable.
But there are semantic differences to these two verbs. Namely that
- PATCH
- Request contains a partial representation of the state to change on a user
Example:
{ "user": { "email": "[email protected]" } }
- POST
- Request contains a full representation of the state to change on a user
Example:
{
"user": {
"id": 1,
"email": "[email protected]",
"full_name": "Gavin Morrice",
// ...
}
}
POST requests should only be used when creating a new resource
A common misconception is that POST requests are only to be used to create a new resource (or a new database record).
Keep reading...POST requests should only be used when creating a new resource
A common misconception is that POST requests are only to be used to create a new resource (or a new database record).
Keep reading...REST is about presenting a CRUD layer with JSON
A lot of people say they are building REST APIs, because their application offers a JSON -based web interface.
Keep reading...Define meta-resources for non-CRUD actions
Because many Rails developers believe they are constrained to 7 RESTful (CRUD) actions, they struggle to understand where they should define actions that fall outside of these defaults. For example, POST users/1/archive
.
Creating a new request should always use POST
A common misconception is that POST requests are only to be used to create a new resource (or a new database record).
Keep reading...There are 7 REST actions
The Ruby on Rails documents themselves mention “the 7 default CRUD actions” (index, new, create, show, edit, update, and destroy).
Keep reading...