views:

40

answers:

1

How do you send "commands" to a RESTful server?

Use case: My server caches certain information so that it does not have to read the database every time that information is requested. I need a way to send a command from my client application to tell the server to flush the cache. Would you use POST or PUT on some URL like ".../flush_cache"?

The "command" is not really data that needs "Representational State Transfer", unless the state being transferred is the result of the command -- "switch is off", "cache is flushed", etc. As a general rule, how does REST send commands to the server?

+1  A: 

I'd suggest this:

  • Create a resource that you can GET which will tell you client how to send the command, similar to an HTML form, using a POST if there are side-effects to this command.
  • POST to the resource to trigger the command and return the URI to a new resource you create during the execution of this POST request (e.g. http://server.example/results/00001), perhaps with a 204 (No Content) status and Location header or a redirect (depending on which client can understand).
  • Let the client check the results on this resource using GET. You may also return the representation for this resource (or something similar) as the entity returned by the POST just before.

It's up to you to decide on the lifecycle of the result resource. It could be short-lived if you don't need to store the results for long. The URI could be constructed from a UUID for example.

Bruno
+1 - I like returning the URI to verify.
James Black
Yes, it's quite useful if the connection breaks or if the command runs for too long. There's a variant of this (if disconnections are a concern) whereby the first POST "stages" the command and the client makes a second similar POST to the returned URI to start the execution, so that you're sure that the client got the initial response with the URI to check.
Bruno