HTTP method to represent “fire and forget” actions in RESTful service

天涯浪子 提交于 2019-12-05 06:14:22

POST would spring to mind, but I think GET is a more appropriate method because 99% of the time you only supply a bunch of parameters to these types of actions. What do you think?

External State

I think that the number of parameters you use has nothing to do with the verb you use. The key issue is are you changing externally visible state?


BatchJob Resources

In your example, if the batch job does not affect the externally visible state of any object then you could implement it as a batch job. However you could model your batch job as a resource with an associated resource container.

You could use a Post to create a new BatchJob resource and allow the user to do a GET to see the progress of the job so far. You could do a GET on the resource container to list all of the running batch jobs, possibly calling DELETE to kill one.

You should use POST if your request modifies data, and GET if it only reads it.

Since your request is "fire and forget", I guess that it's modifying data, so use POST.

I think in the general case we might well supply various payload parameters, and these plausibly might exceed what's possible with GET, so POST is quite reasonable - the action of starting a job doesn't to me fit well with GET sematics.

One thought, might not the action actually return a response:

a). No, sir, that's an impossible request we can't start your job. b). Understood, your job reference is 93.

If you're concerned at that level, perhaps HEAD is the HTTP method you want; it's identical to GET, with the stipulation that the response body is empty. That sounds to me spot-on to what you're asking for?

I'm bringing this question back from the dead to offer a different point of view.

Now that CORS is prevalent, the choice between using GET or POST becomes a matter of if you want anyone who knows your API URI to be able to trigger the batch job (GET), or if you want to restrict the origin of the request to prevent any Joe with a computer from triggering the job (POST).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!