Reputation: 113
I want to invoke a (Java) program that e.g. executes a batch file. That execute command should be invoked through a REST API. Do I just use POST to transmit a json string with e.g. {"command": "do"}? What HTTP method should I use or what is the best solution to do this?
Upvotes: 0
Views: 370
Reputation: 57259
The simple answer: it is okay to use POST.
Advanced answer: how would you do it with a website? You'd probably have an HTML page with a form in it, where the form would allow you to collect whatever information you happen to need from the operator. When the operator hit the submit button, all that information would be converted into an application/x-www-form-urlencoded byte sequence, and sent off to the server in the HTTP request.
The HTTP method used by the request would be the one specified by the method attribute of the form.
In the HTML world, that means that we're looking at GET or POST.
GET suggests (but does not promise) that the result of running the program on some collection of form data produces a representation that can be cached and re-used the next time that same form data is used.
If that doesn't make sense, if you prefer to run the program again rather than re-using a previous result, if you want the side effects of running the program each time, then you probably want POST.
The fact that your server happens to respond to this request by running a java program (or any kind of program) is an accident of your implementation. HTTP methods are about the semantics of the request - what each request means.
The technical details of how you implement your handler for a particular request are deliberately hidden behind the facade that is the uniform interface.
Upvotes: 1