I want to do an HTTP POST
from inside an iMacro to an API endpoint. Effectively, something like the following:
curl -d "data=foo" http://example.com/API
In iMacros, it might look something like this:
my-imacro.iimVERSION BUILD=10.4.28.1074
TAB T=1
URL GOTO=javascript:post('http://example.com/API', {data: 'foo'});
function post(path, params, method) {
// Reference: http://stackoverflow.com/a/133997/1640892
method = method || "post";
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
for (var key in params) {
if (params.hasOwnProperty(key)) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
}
But the above seems like a long and difficult way to do this. If it even works.
Is there a shorter, more direct or efficient solution?
If you are searching for more clean and efficient solution it's need to know that JavaScript will work only in Firefox iMacros plugin. And this script will not work with iMacros plugin version 9.0.3
http://wiki.imacros.net/iMacros_for_Firefox#Version_History
It's better to change API endpoint method to GET. Next you can create iMacros .iim file that extract from web page some properties and send it by GET method to the API endpoint like http://localhost/endpoint?param1=value1¶m2=value2..
You can use http://wiki.imacros.net/iMacros_for_Firefox with javascript and jquery. Then it's easy with any form, get and post request thing.
Small javascript example with jquery and imacros for firefox: