Hi everyone, I want create a script to get information on mikrotik device and post this to my server. I see fetch can create the request but I don’t know how to create POST request with this. On the https://wiki.mikrotik.com/wiki/Manual:Tools/Fetch only tell that fetch can GET. How to create POST request with fetch? Thanks.
Thanks.
This was added in the latest RouterOS 6.39 RC versions
fetch - added “http-data” and “http-method” parameters to allow delete, get, post, put methods (content-type=application/x-www-form-urlencoded by default);
Hi,
adding a content-type for fetch is important and also needed on my end. Most WebServices do require json encoded data and do not accept plain/text.
Not sure if i am thinking wrong, yes i can put json data in the http payload as shown in post #2 but the http header still contains “application/x-www-form-urlencoded”. In my case the application server evaluates the http header and therefore throws an error.
There should be an additional string option for fetch to change the content-type header in the http-request.
Can you please raise the 65535 character limit (double it or remove it completely), so that the error case “max line length 65535 exceeded!” is resolved?
It happens both with GET and POST (I understand that could occur with GET, but it shouldn’t with POST)
As routerOS is more and more involved in the big data domain, it is imperative that such limitations are removed.
This new post feature is great for me.
It adds an extra layer of security and peace of mind when transmitting sensitive information like wifi passwords.
I have a script that all my routers run to check in with my php server. It started as a dyndns client but has evolved into so much more.
Here’s my two cents worth:
First on the router:
# gather basic router info -----------------------------------------------
:local uptime [/system resource get uptime];
:local macadd [/interface wireless get [ find default-name=wlan1 ] mac-address]
:local ver [/system resource get version]
:local name [/system identity get name]
# gather wireless info ---------------------------------------------------
:local wifi [/interface wireless get [ find default-name=wlan1 ] ssid]
:local pw [/interface wireless security-profiles get [ find name=default ] wpa2-pre-shared-key]
# put it all together ----------------------------------------------------
:set $str "rtrName=$name&rtrMac=$macadd&rtrUptime=$uptime&rtrVersion=$ver&ssidName=$wifi&ssidKey=$pw";
# send to server ---------------------------------------------------------
:do {
:put "Checking in";
/tool fetch mode=https url="https://domain.com/checkin.php" keep-result=yes dst-path="result.txt" \
user="routerdevice" password="garbledpassword" http-method="post" http-data=$str ;
} on-error={ log warning "Greeter: Send to server Failed!" }
Save it as greeter.rsc, upload it to your routers and have system scheduler ‘import’ it regularly.
Now on your php server:
//display the received data
print_r($_POST);
// or
foreach ($_POST as $key => $value) {
echo $key . ": " . $value . "<p>\r\n";
}
// Note incoming IP address and timestamp
$checkin_ip = $_SERVER['REMOTE_ADDR'];
$checkin_time = time();
// give a reply
echo "good " . $_SERVER['REMOTE_ADDR'] . "\r\n";
// Now save to a database
Save that as checkin.php and put it where it is reachable on your server.
PHP $_POST expects the same format as the standard http Get:
Just a note here, the fact that a variable doesn’t have such length limitation, makes the fetch data limitation much more awkward. The data can be collected locally in routeros, but they can’t be transmitted! (Well, there can be multiple fetches, but this is a nasty hack, both client and server wise)
Please raise or remove that limitation completely.