POST Request with fetch

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.

You can do it this way:

/tool fetch mode=https url="https://yoursite.com/link/5eu1a1kkypnypbyseqzsq1ao4w"  http-method=post  http-data="payload={\"text\": \"Test\"}"

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);

is there a way to post an array of values? eg a /find something like dhcp leases

the contents should contain data that the other side will understand as an array. You can use a RouterOS script to gather required data on the router.

Wow, this is awesome. Back before this, I always use email to send encoded status in json.

yes, now your router can trigger IFTTT, send Slack and Telegram messaged and so much more.

Is there any way to change “content-type=application/x-www-form-urlencoded” to “content-type=application/json”??

How can I upload file with new fetch feature?
Thanks.

Is it possible to PUT Content-Type: application/json ? I would want to perform the following but i couldn’t.

curl -X PUT "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records/372e67954025e0ba6aaa6d586b9e0b59" \
-H "X-Auth-Email: user@example.com" \
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"example.com","content":"127.0.0.1","ttl":120,"proxied":false}'

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.

You can already do that. Please read the second message in this thread carefully.

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.

How to send header in the method post or get? Option “-H” in curl. For example

curl -H 'PddToken: 123456789ABCDEF0000000000000000000000000000000000000' 'https://pddimp.yandex.ru/api2/admin/dns/list?domain=domain.com'

Hi all

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:

https://domin.com/checkin.php?rtrName=GrandmasHouse&rtrMac=00:11:22:33:44:55&rtrUptime=01:02:03&ssidName=BigBertha&ssidKey=puppies

Hope this helps somebody out.

LL

Found this thread since I wanted to do a POST using /tools fetch…very useful…

But MT should update the docs on it, since http-data etc. isn’t mentioned as a parameter:
https://wiki.mikrotik.com/wiki/Manual:Tools/Fetch

I want to write a script in order to log result of fetch tool , can anyone help me?

Hi, can I upload a text file using fetch

Any news on this max-lenght?

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.