How should the REST API look for updating a resource with server's datetime?

Viewed 440

I am creating a database server with resources with 'lastAccessed' datetime field. This field must be updated with the server's time with every read, but not query. How should I design the REST API if I want to allow clients to update this field at will?

I thought of several options, but none of them seem correct...

  1. GET /api/resources/{resource_id}

    Even though this is a GET, it updates the 'lastAccessed' field.

  2. PATCH /api/resources/{resource_id} with { lastAccessed: "garbage_value" }

    Value from the client is ignored and 'lastAccessed' is updated with the server time.

  3. POST /api/resources/{resource_id}/update-last-accessed-time without payload

    Special endpoint for this purpose agreed on contract

  4. PUT /api/resources/{resource_id}/lastAccessed without payload

    Intention of this API is to convey that this call will replace the 'lastAccessed' field with the server's default value, in this case, server's time.

1 Answers

This is a tricky one. Doing a GET request should typically not influence any meaningful responses.

So I would opt for one of your other three options.

Your POST and PUT examples are fine, although I don't see a reason for sending "garbage_value" to this endpoint.

You could set a flag like setLastAccessed: true. The contents of your PATCH body do not need a 1:1 correlation with anything else.

I would likely go with a POST on /api/resources/{resource_id} or /api/resources/{resource_id}/update-last-accessed-time with a specific instruction to update the lastAccessedTime.

Related