Handy Rails Tips

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).

The intention of the POST HTTP verb is to indicate a non-idempotent request. (RFC-XXX). This usually includes creating new resources, but can also apply to changes that we want to make to existing resources.

For example, consider a use-case where we want to update a record with geolocation coords on each hour.

PATCH client/1

{ "client": { "location": "0.0,0.0" } }

The use of PATCH here communicates the intention that the server should not update the client if the location is not changed since the last time.

Let’s consider an alternative use case: we want to record the changes to the location, _even when the location is the same as it was on the previous request”.

In this case, POST is a more appropriate verb to use:

POST client/1

{ "client": { "location": "0.0,0.0" } }