In my search to understand the HTTP request methods, particularly PUT since I have apparently been using it all wrong, I have stumbled upon a lot of quotes stating “PUT doesn’t belong in REST APIs” and “One should only use GET and POST in modern APIs”.
This just makes me wonder, why would you not use PUT or PATCH or DELETE etc. in REST APIs? Isn’t it what they are there for? To be used because they help with semantics and structure etc.?
Could it have something to do with e.g. the methods receiving the request mainly being methods that directs the data to other methods like the database methods where they are then handled? I’ve used PUT when wanting to update a document, but never has it overwritten a document, even though I only sent part of the data to it.
There is an example of this below, using Express and MongoDB (showing a put method in Express).
app.put('/users/:id', (req, res, next) => {
let id = req.params.id;
userService.getOneUser({_id: ObjectID(id)}).then((user) => {
let updatedData = req.body;
userService.updateUser(user._id, updatedData)
.then((doc) => res.json(doc))
.catch((err) => errorHandler(err, res, next));
}).catch((err) => errorHandler(err, res, next));
})
Basically my question is: In relating to the above statements, how do you use these methods correctly in REST APIs and when do you use them?
EDIT: Example on two references:
PUT doesn't belong in REST API
Only use GET and POST - see third comment on question