Search code examples
httppostgetrequestpatch

What request to use to update resource without sending data


every now and then I come to this question, google around for some time without getting a definitive answer and let it be.

Problem:

I want to update an existing resource, but the logic how this update works is on the backend side.

Let's assume a product should only be visible for a set time. Keeping it simple. The time "from" is stored in the database along the product data: product.visibleFromDate and the duration (eg. 30 days) is just a variable or configured somewhere (not in DB).

Now I want to call the backend telling it to update the visibleFromDate to "now": /api/product/:id/updatevisibility In this case I don't want to send a body because the server should determine what value "now" really is.

I actually don't really care if the server answers with the updated resource or no content.

HTTP Request

GET

  • Should not be used because it should be idempotent, which it wouldn't be because the visibleFromDate would be updated

POST

  • Don't want to send the date from frontend

PUT

  • Don't want to send the date from frontend + should be idempotent

PATCH

  • Merge Patch: Don't want to send the date from frontend
  • JSON Patch: Again, I don't want to send a value, I want the backend to determine it.

Of course I could just send an empty object, the whole resource or some nonsense and ignore it on the backend side, but still I feel like I am missing this "trigger" type of requests which only need the type of resource, id and action.


Solution

  • Depending on the mutability of the data it should either be POST(immutable) or PATCH(mutable). It doesn't depend on what You're sending or not sending.
    If You really want to do it by the book then you should send a '{}' when there are no fields to send. If any is added later you will just add it like '{"duration":"30"}'