Community discussions

 
jamecollins
just joined
Topic Author
Posts: 15
Joined: Tue Apr 11, 2017 1:00 pm

POST Request with fetch

Wed Apr 19, 2017 5:06 am

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.
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 22810
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia

Re: POST Request with fetch

Wed Apr 19, 2017 9:49 am

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);
No answer to your question? How to write posts
 
User avatar
genesispro
Frequent Visitor
Frequent Visitor
Posts: 79
Joined: Fri Mar 14, 2014 12:33 pm

Re: POST Request with fetch

Wed Apr 19, 2017 10:25 am

is there a way to post an array of values? eg a /find something like dhcp leases
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6234
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: POST Request with fetch

Wed Apr 19, 2017 12:15 pm

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.
 
User avatar
ericksetiawan
just joined
Posts: 6
Joined: Fri Nov 20, 2015 6:29 am
Location: localhost

Re: POST Request with fetch

Wed Apr 19, 2017 12:45 pm

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);
Wow, this is awesome. Back before this, I always use email to send encoded status in json.
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 22810
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia

Re: POST Request with fetch

Wed Apr 19, 2017 4:08 pm

yes, now your router can trigger IFTTT, send Slack and Telegram messaged and so much more.
No answer to your question? How to write posts
 
lelmus
just joined
Posts: 14
Joined: Wed Oct 17, 2012 5:50 am

Re: POST Request with fetch

Sat May 06, 2017 7:24 pm

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 any way to change "content-type=application/x-www-form-urlencoded" to "content-type=application/json"??
 
jamecollins
just joined
Topic Author
Posts: 15
Joined: Tue Apr 11, 2017 1:00 pm

Re: POST Request with fetch

Thu May 11, 2017 10:39 am

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);
How can I upload file with new fetch feature?
Thanks.
 
chaplin
just joined
Posts: 3
Joined: Mon Jul 21, 2014 4:30 pm

Re: POST Request with fetch

Thu May 11, 2017 5:52 pm

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}'
 
jobear
just joined
Posts: 2
Joined: Wed Jun 14, 2017 7:03 pm

Re: POST Request with fetch

Wed Jun 14, 2017 7:06 pm

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.
 
andriys
Forum Guru
Forum Guru
Posts: 1010
Joined: Thu Nov 24, 2011 1:59 pm
Location: Kharkiv, Ukraine

Re: POST Request with fetch

Wed Jun 14, 2017 10:09 pm

adding a content-type for fetch is important and also needed on my end
You can already do that. Please read the second message in this thread carefully.
 
jobear
just joined
Posts: 2
Joined: Wed Jun 14, 2017 7:03 pm

Re: POST Request with fetch

Fri Jun 16, 2017 4:24 pm

adding a content-type for fetch is important and also needed on my end
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.
 
thantoldo
just joined
Posts: 20
Joined: Tue Apr 10, 2012 10:08 pm

Re: POST Request with fetch

Mon Jul 03, 2017 8:19 pm

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.
 
User avatar
ADvorkin
just joined
Posts: 1
Joined: Mon Jul 10, 2017 1:30 pm

Re: POST Request with fetch

Mon Jul 10, 2017 1:36 pm

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'
 
LaZyLion
just joined
Posts: 23
Joined: Fri May 09, 2014 10:27 am

Re: POST Request with fetch

Sun Jul 16, 2017 8:21 pm

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
 
Amm0
just joined
Posts: 3
Joined: Sun May 01, 2016 7:12 pm

Re: POST Request with fetch

Tue Aug 22, 2017 7:30 am

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
 
jelyasi
just joined
Posts: 1
Joined: Thu Sep 28, 2017 5:31 pm

Re: POST Request with fetch

Thu Sep 28, 2017 5:37 pm

I want to write a script in order to log result of fetch tool , can anyone help me?
 
jamecollins
just joined
Topic Author
Posts: 15
Joined: Tue Apr 11, 2017 1:00 pm

Re: POST Request with fetch

Thu Oct 12, 2017 11:06 am

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);
Hi, can I upload a text file using fetch
 
User avatar
Setneuf
newbie
Posts: 30
Joined: Sun Oct 22, 2017 7:22 pm
Location: Pombal, PORTUGAL
Contact:

Re: POST Request with fetch

Tue Oct 24, 2017 2:48 pm

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.
Any news on this max-lenght?
Working with Mikrotik since 2007
 
thantoldo
just joined
Posts: 20
Joined: Tue Apr 10, 2012 10:08 pm

Re: POST Request with fetch

Wed Oct 25, 2017 11:38 am

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.
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.
 
User avatar
Setneuf
newbie
Posts: 30
Joined: Sun Oct 22, 2017 7:22 pm
Location: Pombal, PORTUGAL
Contact:

Re: POST Request with fetch

Fri Oct 27, 2017 11:14 am

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.
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.
They had the limitation for sure because the GET and forgot to remove it when they implemented the POST, I believe that in a future release this will be fixed 8)
Working with Mikrotik since 2007
 
svda
just joined
Posts: 2
Joined: Fri May 22, 2015 8:36 am

Re: POST Request with fetch

Thu Dec 07, 2017 8:03 pm

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'
Encouraging an idea of fetching with custom headers. pdd.yandex.ru (name server api) requires using fields in the header
 
Xenhat
just joined
Posts: 1
Joined: Fri Dec 30, 2016 9:41 pm

Re: POST Request with fetch

Sat Jan 13, 2018 5:42 pm

I would like to reinforce the need for this functionality (specifically cURL's
-H
custom header parameter for the same reasons.
Cloudflare is a widely used service and their V1 API seems to have been non-functional for write operations (and deprecated for several months if not years).
The inability to use these headers means we can no longer update DNS records from RouterOS itself, a feature extremely handy for users who are still stuck with dynamic IPs.
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}'
 
sheahant
just joined
Posts: 6
Joined: Mon Mar 24, 2014 9:35 am

Re: POST Request with fetch

Mon Jan 29, 2018 6:46 am

I absolutely agree with above posts asking for header customization.

In the course of doing any diverse amount of API work, specifying the content-type header and then arbitrary headers inevitably come up as necessary features.

A bit further along, in some cases, comes the need to be able to read http-code, response-data and sometimes response-headers from the server reply - but request headers are fundamental.

Who is online

Users browsing this forum: Xtremer and 2 guests