Community discussions

MikroTik App
 
jamecollins
just joined
Topic Author
Posts: 18
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: 26322
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);
 
User avatar
genesispro
Member Candidate
Member Candidate
Posts: 283
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: 6263
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
Trainer
Trainer
Posts: 10
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: 26322
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.
 
lelmus
newbie
Posts: 28
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: 18
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: 1526
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: 22
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
newbie
Posts: 32
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
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

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: 18
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: 37
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?
 
thantoldo
just joined
Posts: 22
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: 37
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)
 
svda
just joined
Posts: 3
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: 8
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.
 
Dem0n3D
just joined
Posts: 1
Joined: Tue Mar 06, 2018 8:38 pm

Re: POST Request with fetch

Tue Mar 06, 2018 8:40 pm

We need HEADERS!
 
svda
just joined
Posts: 3
Joined: Fri May 22, 2015 8:36 am

Re: POST Request with fetch

Thu Mar 08, 2018 2:30 pm

As I see, it is already in release candidate
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 26322
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia

Re: POST Request with fetch

Thu Mar 08, 2018 2:42 pm

 
lukaszg
just joined
Posts: 17
Joined: Thu Apr 28, 2016 10:46 am

Re: POST Request with fetch

Thu Mar 08, 2018 10:07 pm

This is for content type only right? To use it with the cloudflare we still need to wait for the headers support?
 
jvianelfamilia
just joined
Posts: 1
Joined: Thu Mar 15, 2018 2:57 pm

Re: POST Request with fetch

Thu Mar 15, 2018 5:23 pm

Hello, some body can help me on how can i call this url "http://192.168.90.6/wifi/index.php/wifi ... arClientes" with fetch and make a schedule with this script execute auto every 5 minutes?

thanks in advange!
 
awonglk
newbie
Posts: 33
Joined: Sat Oct 31, 2015 3:43 pm

Re: POST Request with fetch

Sat Apr 07, 2018 6:25 pm

Can I get an idea when will "header" support be added to fetch tool?
This is quite useful for accessing API data which requires headers for authorization. Example by Xenhat already demonstrates this with DNS updates through Cloudflare which requires passing header details relating to Authorization in order to use their API.

I am not sure if svda is correct, in that header support is already available in release candidate already. I don't see mentions of it in the Changelog for 6.42rc52.
 
pafflootiy
just joined
Posts: 10
Joined: Tue Sep 23, 2014 10:27 am

Re: POST Request with fetch

Thu Apr 19, 2018 7:25 pm

:local chann dude-1
:local notification "Time: [Time]\nAddress: [Device.CustomField2]\nIP: [Device.FirstAddress]\nPing: [Service.Status]"
:local iconurl https://wiki.mikrotik.com/images/5/54/Dude-icon3.png
:local pload "payload={\"channel\": \"#$chann\", \"username\": \"DUDE-server\", \"text\": \"$notification\", \"icon_url\": \"$iconurl\"}"

/tool fetch http-method=post http-content-type="application/json" http-data=$pload url="https://hooks.slack.com/services/TXXXXXXXX/YYYYYYYYY/ZZZZZZZZZZZZZZZZZ" keep-result=no;
My script to send slack notifications
 
godlike
just joined
Posts: 21
Joined: Mon Jul 15, 2013 6:41 pm

Re: POST Request with fetch

Sun Apr 29, 2018 1:48 pm

A lot of APIs need custom headers for authentication.
Please, add ability to specify them.
Thanks!
 
xincun
just joined
Posts: 2
Joined: Tue Apr 17, 2018 1:27 pm

Re: POST Request with fetch

Wed May 02, 2018 7:06 am

Can I get an idea when will "header" support be added to fetch tool?
This is quite useful for accessing API data which requires headers for authorization. Example by Xenhat already demonstrates this with DNS updates through Cloudflare which requires passing header details relating to Authorization in order to use their API.

I am not sure if svda is correct, in that header support is already available in release candidate already. I don't see mentions of it in the Changelog for 6.42rc52.
Does rc already support "-H"? How to convert curl to fetch? Any example?

curl -X PUT "https://api.cloudflare.com/client/v4/zo ... 586b9e0b59" \
-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}'

/tool fetch http-method=put http-content-type="application/json" http-data="{"type":"A","name":"example.com","content":"127.0.0.1","ttl":120,"proxied":false}" url="https://api.cloudflare.com/client/v4/zo ... 586b9e0b59"

How to convert “-H”?
 
Vargas
just joined
Posts: 6
Joined: Fri Jun 01, 2018 5:44 pm

Re: POST Request with fetch

Fri Jun 01, 2018 5:53 pm

HEADERS are definitely a fundamental requirement in FETCH. Please consider at least the basic and standard headers.

For instance to send an SMS with content YYY to MSISDN +39XXX on connected russian Beeline ZTE MF831 LTE USB sticks you need to define the Referer.

With wget:
wget --post-data="isTest=false&goformId=SEND_SMS&notCallback=true&Number=%2B393XXX&MessageBody=YYY&ID=-1&encode_type=GSM7_default" --referer="http://192.168.0.1/index.html" http://192.168.0.1/goform/goform_set_cmd_process

An SMS message is sent.

With Fetch:
/tool fetch mode=http url="http://192.168.0.1/goform/goform_set_cmd_process" http-method=post http-data="isTest=false&goformId=SEND_SMS&notCallback=true&Number=%2B39XXX&MessageBody=YYY&ID=-1&encode_type=GSM7_default" output=none

No referer header, therefore the command fails and no SMS from RouterOS.

Best regards
 
krizzo
just joined
Posts: 3
Joined: Thu May 17, 2018 4:12 am

Re: POST Request with fetch

Mon Jul 30, 2018 9:12 pm

I looked through all the changelogs for RC as well as the current releases. I couldn't find anything indicating that fetch is getting the option for customizing headers. Is this feature something that will be added in the future?
 
lustyffh
newbie
Posts: 39
Joined: Thu Jul 05, 2012 11:46 pm

Re: POST Request with fetch

Wed Aug 29, 2018 11:55 pm

just heads up to this thread! I need digest authentication on post/get. and seems like Mikrotik doesn't support even that thing.
 
User avatar
колбаскин
newbie
Posts: 40
Joined: Tue Mar 29, 2016 6:36 pm
Location: Ukraine Zaporozhye
Contact:

Re: POST Request with fetch

Wed Dec 12, 2018 6:53 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}'
Yes! Need this!
How to add -H "X-Auth-Email ?
Cloudflare not work (((
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 26322
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia

Re: POST Request with fetch

Thu Dec 13, 2018 11:06 am

Next RouterOS v6.44 beta will have custom header option
 
User avatar
колбаскин
newbie
Posts: 40
Joined: Tue Mar 29, 2016 6:36 pm
Location: Ukraine Zaporozhye
Contact:

Re: POST Request with fetch

Fri Dec 14, 2018 3:08 pm

6.44beta40 - this?
Or when?

I wrote an intermediate php script to update. I would like to do it right from the router

cloudflare.php
<?php
$zones = isset($_GET['zones']) ? trim($_GET['zones']) : exit;
$dns_records = isset($_GET['dns_records']) ? trim($_GET['dns_records']) : exit;
$zones = $_GET['zones'];
$dns_records = $_GET['dns_records'];
$name = $_GET['name'];
$content = $_GET['content'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/" . $zones . "/dns_records/" . $dns_records);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"type\":\"A\",\"name\":\"" . $name . "\",\"content\":\"" . $content . "\",\"ttl\":120,\"proxied\":false}");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$headers = array();
$headers[] = "X-Auth-Email: max@hd.zp.ua";
$headers[] = "X-Auth-Key: 1c2c585915534145fecded4526e8668177a33";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
var_dump($result);
curl_close ($ch);
?>
Need send to cloudflare.php 4 parameters
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 26322
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia

Re: POST Request with fetch

Fri Dec 14, 2018 3:23 pm

no, 40 is the current one. the next one will be possibly 50
 
User avatar
колбаскин
newbie
Posts: 40
Joined: Tue Mar 29, 2016 6:36 pm
Location: Ukraine Zaporozhye
Contact:

Re: POST Request with fetch

Wed Dec 19, 2018 11:21 am

http-header-field= - allows you to specify only once.
How to specify 3 headers?

-H "X-Auth-Email: max@hd.zp.ua" \
-H "X-Auth-Key: 1c2c585915534145fecded4526e8668177a33" \
-H "Content-Type: application/json" \
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 26322
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia

Re: POST Request with fetch

Wed Dec 19, 2018 11:28 am

http-header-field= - allows you to specify only once.
How to specify 3 headers?

-H "X-Auth-Email: max@hd.zp.ua" \
-H "X-Auth-Key: 1c2c585915534145fecded4526e8668177a33" \
-H "Content-Type: application/json" \
Yes, currently only one. We will make multiple possible, check one of the new betas after New Year.
 
pista
just joined
Posts: 9
Joined: Thu Aug 04, 2016 12:46 pm

Re: POST Request with fetch

Fri Jan 18, 2019 7:22 pm

http-header-field= - allows you to specify only once.
How to specify 3 headers?

-H "X-Auth-Email: max@hd.zp.ua" \
-H "X-Auth-Key: 1c2c585915534145fecded4526e8668177a33" \
-H "Content-Type: application/json" \
Yes, currently only one. We will make multiple possible, check one of the new betas after New Year.

18/01/2019 and still no multiple fetch header fields :( :( :(
 
User avatar
колбаскин
newbie
Posts: 40
Joined: Tue Mar 29, 2016 6:36 pm
Location: Ukraine Zaporozhye
Contact:

Re: POST Request with fetch

Fri Jan 18, 2019 8:55 pm

18/01/2019 and still no multiple fetch header fields :( :( :(
I put the intermediate script on php higher on the forum. You can download it to any server viewtopic.php?f=9&t=120860&e=1&view=unread#p703390

/tool fetch mode=https url="https://hd.zp.ua/cloudflare.php\?zones=$CFzones&dns_records=$CFtdnsrecords&name=$hostname&content=$externalIP" keep-result=no
 
User avatar
bekax5
Member Candidate
Member Candidate
Posts: 110
Joined: Thu Apr 30, 2015 11:27 pm

Re: POST Request with fetch

Tue Feb 26, 2019 5:05 am

In the meantime this feature has been added.
Has anyone tried to use it with the Cloudflare v4 API ?

I am having difficulties with post to update a DNS zone.
It gives me error 400 bad request.

[admin@MikroTik-RB3011] > /tool fetch http-method=get url="https://api.cloudflare.com/client/v4/zones/__ZONEID__/dns_records/__ID__" http-header-field=X-Auth-Email:__EMAIL__,X-Auth-Key:__AUTH_KEY__,http-content-type:application/json output=none
      status: finished
  downloaded: 0KiBC-z pause]
    duration: 1s
    
[admin@MikroTik-RB3011] > /tool fetch http-method=post url="https://api.cloudflare.com/client/v4/zones/__ZONEID__/dns_records/__ID__" http-header-field=X-Auth-Email:__EMAIL__,X-Auth-Key:__AUTH_KEY__,http-content-type:application/json output=none http-data="{\"type\":\"A\",\"name\":\"__A_RECORD__\",\"content\":\"192.168.1.1\"}"
  status: failed
  failure: closing connection: <400 Bad Request> 104.19.198.151:443 (4)
 
pista
just joined
Posts: 9
Joined: Thu Aug 04, 2016 12:46 pm

Re: POST Request with fetch

Tue Feb 26, 2019 5:08 pm

In the meantime this feature has been added.
Has anyone tried to use it with the Cloudflare v4 API ?

I am having difficulties with post to update a DNS zone.
It gives me error 400 bad request.

[admin@MikroTik-RB3011] > /tool fetch http-method=get url="https://api.cloudflare.com/client/v4/zones/__ZONEID__/dns_records/__ID__" http-header-field=X-Auth-Email:__EMAIL__,X-Auth-Key:__AUTH_KEY__,http-content-type:application/json output=none
      status: finished
  downloaded: 0KiBC-z pause]
    duration: 1s
    
[admin@MikroTik-RB3011] > /tool fetch http-method=post url="https://api.cloudflare.com/client/v4/zones/__ZONEID__/dns_records/__ID__" http-header-field=X-Auth-Email:__EMAIL__,X-Auth-Key:__AUTH_KEY__,http-content-type:application/json output=none http-data="{\"type\":\"A\",\"name\":\"__A_RECORD__\",\"content\":\"192.168.1.1\"}"
  status: failed
  failure: closing connection: <400 Bad Request> 104.19.198.151:443 (4)

To update DNS zone, you must use PUT method. Replace http-method=post with http-method=put and add mode=https
 
User avatar
bekax5
Member Candidate
Member Candidate
Posts: 110
Joined: Thu Apr 30, 2015 11:27 pm

Re: POST Request with fetch

Tue Feb 26, 2019 7:11 pm

In the meantime this feature has been added.
Has anyone tried to use it with the Cloudflare v4 API ?

I am having difficulties with post to update a DNS zone.
It gives me error 400 bad request.

[admin@MikroTik-RB3011] > /tool fetch http-method=get url="https://api.cloudflare.com/client/v4/zones/__ZONEID__/dns_records/__ID__" http-header-field=X-Auth-Email:__EMAIL__,X-Auth-Key:__AUTH_KEY__,http-content-type:application/json output=none
      status: finished
  downloaded: 0KiBC-z pause]
    duration: 1s
    
[admin@MikroTik-RB3011] > /tool fetch http-method=post url="https://api.cloudflare.com/client/v4/zones/__ZONEID__/dns_records/__ID__" http-header-field=X-Auth-Email:__EMAIL__,X-Auth-Key:__AUTH_KEY__,http-content-type:application/json output=none http-data="{\"type\":\"A\",\"name\":\"__A_RECORD__\",\"content\":\"192.168.1.1\"}"
  status: failed
  failure: closing connection: <400 Bad Request> 104.19.198.151:443 (4)

To update DNS zone, you must use PUT method. Replace http-method=post with http-method=put and add mode=https
Thanks for the reply!!

Lol, I actually just missed that hahah.
Even though the error still persists, am I still missing something ?
[admin@MikroTik-RB3011] > /tool fetch http-method=put mode=https url="https://api.cloudflare.com/client/v4/zones/__ZONEID__/dns_records/__ID__" http-header-field=X-Auth-Email:__EMAIL__,X-Auth-Key:__AUTHKEY__,http-content-type:application/json output=none http-data="{\"type\":\"A\",\"name\":\"__ARECORD__\",\"content\":\"192.168.1.1\"}"
  status: failed

failure: closing connection: <400 Bad Request> 104.19.195.151:443 (4)
 
pista
just joined
Posts: 9
Joined: Thu Aug 04, 2016 12:46 pm

Re: POST Request with fetch

Tue Feb 26, 2019 8:29 pm

In the meantime this feature has been added.
Has anyone tried to use it with the Cloudflare v4 API ?

I am having difficulties with post to update a DNS zone.
It gives me error 400 bad request.

[admin@MikroTik-RB3011] > /tool fetch http-method=get url="https://api.cloudflare.com/client/v4/zones/__ZONEID__/dns_records/__ID__" http-header-field=X-Auth-Email:__EMAIL__,X-Auth-Key:__AUTH_KEY__,http-content-type:application/json output=none
      status: finished
  downloaded: 0KiBC-z pause]
    duration: 1s
    
[admin@MikroTik-RB3011] > /tool fetch http-method=post url="https://api.cloudflare.com/client/v4/zones/__ZONEID__/dns_records/__ID__" http-header-field=X-Auth-Email:__EMAIL__,X-Auth-Key:__AUTH_KEY__,http-content-type:application/json output=none http-data="{\"type\":\"A\",\"name\":\"__A_RECORD__\",\"content\":\"192.168.1.1\"}"
  status: failed
  failure: closing connection: <400 Bad Request> 104.19.198.151:443 (4)

To update DNS zone, you must use PUT method. Replace http-method=post with http-method=put and add mode=https
Thanks for the reply!!

Lol, I actually just missed that hahah.
Even though the error still persists, am I still missing something ?
[admin@MikroTik-RB3011] > /tool fetch http-method=put mode=https url="https://api.cloudflare.com/client/v4/zones/__ZONEID__/dns_records/__ID__" http-header-field=X-Auth-Email:__EMAIL__,X-Auth-Key:__AUTHKEY__,http-content-type:application/json output=none http-data="{\"type\":\"A\",\"name\":\"__ARECORD__\",\"content\":\"192.168.1.1\"}"
  status: failed

failure: closing connection: <400 Bad Request> 104.19.195.151:443 (4)

OK :) In header field replace http-content-type:application/json with content-type:application/json
 
User avatar
bekax5
Member Candidate
Member Candidate
Posts: 110
Joined: Thu Apr 30, 2015 11:27 pm

Re: POST Request with fetch

Tue Feb 26, 2019 8:32 pm

OK :) In header field replace http-content-type:application/json with content-type:application/json
Hahahah, nice catch!
I've been trying this on yesterday's late night and I probably wasn't even looking anymore.

Thanks a lot, it worked perfectly.
 
Cliff
just joined
Posts: 9
Joined: Fri Jul 05, 2013 1:57 am

Re: POST Request with fetch

Thu Feb 28, 2019 9:56 am

Has anyone sent messages to Rocketchat?
I have the curl:
curl -X POST -H 'Content-Type: application/json' --data '{"text":"Example"}' http://myip:3000/hooks/SHFuHFpDFHFOEFHOI5984w798q
And it work perfectly in linux.
Trying to convert in into fetch:
/tool fetch keep-result=no mode=http url="http://myip:3000/hooks/SHFuHFpDFHFOEFHOI5984w798q" http-method=post http-content-type="application/json" http-data="\"text\":\"Example message\""
But receive error:
failure: closing connection: <400 Bad Request> myip:3000 (4)
Last edited by Cliff on Thu Feb 28, 2019 11:27 am, edited 1 time in total.
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 26322
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia

Re: POST Request with fetch

Thu Feb 28, 2019 11:10 am

Your http-data doesn't look like JSON at all, you only have quotes in there.
 
Cliff
just joined
Posts: 9
Joined: Fri Jul 05, 2013 1:57 am

Re: POST Request with fetch

Thu Feb 28, 2019 11:28 am

My bad, forgot { }. Thanks.
 
WeWiNet
Long time Member
Long time Member
Posts: 592
Joined: Thu Sep 27, 2018 4:11 pm

Re: POST Request with fetch

Thu Feb 28, 2019 3:10 pm

Hi jumping on this thread.

I cannot get to the Webhook/IFTTT with the indicated command by Normis:

/tool fetch mode=https url="https://maker.ifttt.com/use/xvcvvxvvcvvxvxvxvv" http-method=post


It gives me error:
status: failed
failure: closing connection: <404 Not Found>

Also tried with additional user info usr=... but same result

What would be the correct syntax to trigger an IFTTT event in IFTTT/Webhooks?

Thanks in advance
---
ROS: 6.44 on RB4011
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 26322
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia

Re: POST Request with fetch

Thu Feb 28, 2019 3:19 pm

You probably need a different URL, log into IFTTT and visit this page:
https://ifttt.com/maker_webhooks

After logged in and connected, you should see. the Documentation button on the top, it will give you your URL and API KEY
 
WeWiNet
Long time Member
Long time Member
Posts: 592
Joined: Thu Sep 27, 2018 4:11 pm

Re: POST Request with fetch

Thu Feb 28, 2019 4:45 pm

Normis,

this is just a key replacement for the real value I have put there.
The Webhook link itself is correct. just copied it again from IFTTT.

Do you need user name or some other setting?

thanks for your help.
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 26322
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia

Re: POST Request with fetch

Thu Feb 28, 2019 4:58 pm

No, it should be enough with that link
 
WeWiNet
Long time Member
Long time Member
Posts: 592
Joined: Thu Sep 27, 2018 4:11 pm

Re: POST Request with fetch

Fri Mar 01, 2019 10:07 am

Thanks Normis, your comment made me continue till I found the issue!

To share with others here the full guide to do IFTTT with RouterOS on Mikrotik:
  • head over to IFTTT and sign up
  • add as service "Webhooks" and go to Webhooks --> settings
    this will give you something like:
    • Connected as: YourGoogleName (you will not need this later on)
    • URL: https://maker.ifttt.com/use/your-code

Now comes the simple, yet tricky thing I missed earlier!!!
  • You need to FOLLOW ABOVE URL (enter it into web browser), to set up the real https link to be used to trigger IFTTT
  • You will see an easy config example with an empty field to be filled by you with YOUR-EVENT-TAG:
    https://maker.ifttt.com/trigger/YOUR-EVENT-TAG/with/key/your-code
    at the same time you can add JSON data in the value fields (not needed to do simple IFTTT trigger.
    You will also see a CURL... example at the bottom, which you can directly run in Linux or on MacOS to trigger the event.
    When finished setting up the TAG copy the new complete https link.
  • In routeros you now can use following command to trigger IFTTT (can then be used in script etc.)
    /tool fetch https://maker.ifttt.com/trigger/YOUR-EVENT-TAG/with/key/your-code keep-result=no
    If successful you will see no error :-) (the data transfert is 0, but this is normal as no data is send).

  • in Your IFTTT applet / recipe you can now use the YOUR-EVENT-TAG to trigger something else...

Hope this helps others


Needless to say, this will only work if google servers are reachable (mikrotik connected to internet) and no firewall rule prevents https traffic

PS: Sorry for the horrible formatting, I have no clue how to use forum editing features. If one has a good guide I am happy to take it...
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 26322
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia

Re: POST Request with fetch

Fri Mar 01, 2019 10:55 am

Thanks for sharing the full guide, it will be useful to others who want to use IFTTT
 
User avatar
колбаскин
newbie
Posts: 40
Joined: Tue Mar 29, 2016 6:36 pm
Location: Ukraine Zaporozhye
Contact:

Re: POST Request with fetch

Fri Mar 01, 2019 10:56 am

http-header-field= - allows you to specify only once.
How to specify 3 headers?

-H "X-Auth-Email: max@hd.zp.ua" \
-H "X-Auth-Key: 1c2c585915534145fecded4526e8668177a33" \
-H "Content-Type: application/json" \
Yes, currently only one. We will make multiple possible, check one of the new betas after New Year.
How about this?
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 26322
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia

Re: POST Request with fetch

Fri Mar 01, 2019 11:03 am

Multiple headers already possible for a few versions, even documented in Wiki:
https://wiki.mikrotik.com/wiki/Manual:T ... Properties

It's also discussed in this topic:
viewtopic.php?f=9&t=120860&p=709811&hil ... rs#p717584
 
ivanfm
newbie
Posts: 48
Joined: Sun May 20, 2012 5:07 pm

Re: POST Request with fetch

Fri Mar 01, 2019 4:18 pm

This things will be better when authentications like digest is implemented for fetch command because some new devices are not supporting more the basic authentication.
 
WeWiNet
Long time Member
Long time Member
Posts: 592
Joined: Thu Sep 27, 2018 4:11 pm

Re: POST Request with fetch

Tue Mar 05, 2019 4:08 pm

Normis,

I tried to get the 3 values posted from routerOS into IFTTT without success.
And all the application/json examples are pre 6.44 so I am lost after trying lot of options.
Same for WIKI, still using the http-content-type option which is no more available in 6.44.

The working CURL code from IFTTT is

Code: Select all

curl -X POST -H "Content-Type: application/json" -d '{"value1":"RB4100","value2":"Shutdown","value3":"DSL"}' https://maker.ifttt.com/trigger/mtik/with/key/my_IFTTT_code
--> gives perfect result

The RouterOS command I use based on this (and tried tons of other variants) is:

Code: Select all

/tool fetch https://maker.ifttt.com/trigger/mtik/with/key/my_IFTTT_code keep-result=no
http-header-field=http-content-type:application/json http-data="{\"value1\":\"RB4011\",\"value2\":\"shutting down\",\"value3\":\"DSL"}"
I get the trigger in IFTTT but all my tests show empty values in IFTTT except if I run the CURL command above.
Any advice?

thanks
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7042
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: POST Request with fetch

Tue Mar 05, 2019 5:06 pm

http-header-field="Content-Type: application/json"
 
WeWiNet
Long time Member
Long time Member
Posts: 592
Joined: Thu Sep 27, 2018 4:11 pm

Re: POST Request with fetch

Tue Mar 05, 2019 6:46 pm

Does not work with this command. Puts an error:
/tool fetch https://maker.ifttt.com/trigger/mtik/with/key/my_IFTTT_code keep-result=no \
http-header-field="Content-Type:application/json" \
http-data="{\"value1\":\"RB4011\",\"value2\":\"shutting down\",\"value3\":\"DSL"}"
It spits error out:
status: failed
failure: closing connection: <400 Bad Request> 34.201.148.91:443 (4)
has someone used IFTTT recently with values under with 6.44
 
taxist
just joined
Posts: 1
Joined: Thu Mar 14, 2019 6:08 pm

Re: POST Request with fetch

Fri Mar 15, 2019 9:47 am

To owerload content type need use directive http-header-field and set key content-type in lower case, example
http-header-field="content-type:text/plain"
 
WeWiNet
Long time Member
Long time Member
Posts: 592
Joined: Thu Sep 27, 2018 4:11 pm

Re: POST Request with fetch

Thu Apr 04, 2019 12:39 pm

Hi taxist,

thanks for your advice, but the IFTTT webhook data does not work neither with this.
The issue is always that either routeros does not accept the syntax that IFTTT would need and gives you an error,
or if you put it in routeros syntax, webhook does not get/display the data.

Do you have a full command example with data that works?
 
tempoff
just joined
Posts: 4
Joined: Fri May 10, 2019 1:47 pm

Re: POST Request with fetch

Fri May 10, 2019 1:51 pm

Hello everyone!

Can you also add a patch http method to fetch tool please? It can be very useful.
 
bruna
just joined
Posts: 1
Joined: Thu May 16, 2019 9:14 pm

mikrotik does not recognize intel network card

Fri May 17, 2019 4:02 pm

Hi

mikrotik does not recognize intel network card.



I installed the Mikrotik software on a hardware and after installation Mikrotik does not recognize the network card.

Could someone help?


any alternative?

thanks !
 
User avatar
rilliam
newbie
Posts: 48
Joined: Thu Mar 12, 2009 7:34 pm

Re: POST Request with fetch

Tue Aug 20, 2019 10:48 am

To owerload content type need use directive http-header-field and set key content-type in lower case, example
http-header-field="content-type:text/plain"
Wow wish this was on the man page for fetch. I just wasted two nights trying to figure this out.

If you don't use lower case, fetch will send two header-fields for content-type, one with the correct MIME type and the other with default webform MIME type.
 
iqbaldalban
just joined
Posts: 4
Joined: Sat Aug 24, 2019 11:55 am
Contact:

Re: POST Request with fetch

Sat Aug 24, 2019 12:11 pm

hello everyone, in the past few days I have experienced a problem with the fetch script that suddenly doesn't want to run anymore. maybe someone here knows the cause.
This is an example of a fetch script that I use to auto login to the wifi captive portal
{
log error ("Internet Mati --- Memulai Koneksi Ulang ......")
/ip dns cache flush
/ip dhcp-client release [find interface=wlan2]
log warning ("DHCP Client Baru")
delay 10
:local ip [/ip address get [/ip address find interface="wlan2"] address];
:local macadd [/interface wireless get [ find default-name=wlan2 ] mac-address];
:local ip [put [:pick $ip 0 [:find $ip "/"]]]
/tool fetch http-method=post http-data="username_=DalBan&username=DalBan.lPgC%40freeMS&password=DalBan" url=("https://welcome2.wifi.id/wms/auth/authnew/autologin/quarantine.php?ipc=$ip&gw_id=WAG-D4-GBL&mac=$macadd&wlan=TGSLW00005/TLK-WI124968-0001:UN%20CHECK") keep-result=no
log warning ("Internet Sudah Menyala")
}
and this is the current data fetch that I took through the chrome network
fetch("https://welcome2.wifi.id/wms/auth/authnew/autologin/quarantine.php?ipc=10.148.103.193&gw_id=WAG-D4-GBL&mac=b4:cb:57:6f:7a:91&redirect=http://8.8.8.8/&wlan=TGSLW00005/TLK-WI124968-0001:RUMAH%20IQBAL&landURL=", {"credentials":"include","headers":{"accept":"application/json, text/javascript, */*; q=0.01","accept-language":"en","content-type":"application/x-www-form-urlencoded; charset=UTF-8","sec-fetch-mode":"cors","sec-fetch-site":"same-origin","x-requested-with":"XMLHttpRequest"},"referrer":"https://welcome2.wifi.id/wms/?gw_id=WAG-D4-GBL&client_mac=b4:cb:57:6f:7a:91&wlan=TGSLW00005/TLK-WI124968-0001:RUMAH%20IQBAL&sessionid=0800FFFF7802E286-5D60F78B&redirect=http://8.8.8.8/","referrerPolicy":"no-referrer-when-downgrade","body":"username_=DalBan&username=DalBan.xJjh%40freeMS&password=DalBan","method":"POST","mode":"cors"});
 
brewer7
just joined
Posts: 2
Joined: Fri Dec 06, 2019 4:50 pm

Re: POST Request with fetch

Sat Dec 07, 2019 9:31 am

Guys, can you help me out here.

i'm trying to make a script to send text (SMS) via hilink Huawei E8372. i've found a few solutions on CURL like this or this. I've decided to rework this script for Mikrotik (i have RB951 ROS v6.45.3).

I've stucked truing to get authorized on the Web interface.
here wthat i have for now:
/tool fetch url="http://192.168.8.1/api/webserver/SesTokInfo" dst-path="ses_tok.txt"
global content [/file get [/file find name=ses_tok.txt] contents] 
  :local COOKIE [:pick $content 71 199]
  :local TOKEN [:pick $content 220 252]
  :local LOGIN "<request><Username>admin</Username><Password>admin</Password><password_type>3</password_type></request>"

log info "COOKIE: $COOKIE"
log info "TOKEN: $TOKEN"

/tool fetch mode=http http-method=post url="http://192.168.8.1/api/user/login" http-data=($LOGIN) http-header-field=(Cookie:$COOKIE,__RequestVerificationToken:$TOKEN,content-type:text/xml,Connection:keep-alive) dst-path="login_resp_hdr.txt" keep-result=no
but no file after 2nd fetch command downloaded.

In the meantime in log:
18:17:58 info fetch: file "ses_tok.txt" downloaded 
18:17:58 script,info COOKIE: hoVS1kv8/waT0S4OyMrwZQS5OFOymv1Ruy6XovYaLK7xffO2v2mjP
PctukTNEuc7NhK1jdqE0X8c/DjIxyWMrqKmp6K3x74n1ja8xrVY/pMJCg3YJzXf1jwMIybRaxG7 
18:17:58 script,info TOKEN: 1L719lqR/w/T/XnOu+pu3JBj75e3EIAH
i beulive the issue is in how i describe http-header-field. What am i doing wrong?
 
oneblade
just joined
Posts: 1
Joined: Wed Feb 19, 2020 11:45 pm

Re: POST Request with fetch

Wed Feb 19, 2020 11:51 pm

Hello
How to replace the linux script shown below to make it work in mikrotik? The script updates the dmz host on the ISP's router.
#!/bin/bash
curl -u admin:password \
-c cookie_router \
-X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "SelectLanguage=en_US&LogoutApplyAction=&DMZHostIP0=192.168.1.200&RouterIPAddress=192.168.1.1" \
http://192.168.1.1/goform/AdvancedDMZHost
I will be grateful for help because I am just starting with mikrotik
 
kai
newbie
Posts: 38
Joined: Thu Aug 24, 2017 1:15 pm

Re: POST Request with fetch

Wed Mar 11, 2020 8:03 pm

OK :) In header field replace http-content-type:application/json with content-type:application/json
Hahahah, nice catch!
I've been trying this on yesterday's late night and I probably wasn't even looking anymore.

Thanks a lot, it worked perfectly.
Sorry to bring up a post from the dead.. but I can't find anything more recent.

I'm having some trouble with Cloudflare, getting a 400 Bad request, yet when I do it manually through cURL, it will work successfully, just not through RouterOS.

These are the commands:
/tool fetch mode=https http-method=put url="https://api.cloudflare.com/client/v4/zones/__ZONEID__/dns_records" http-header-field="content-type:application/json,X-Auth-Email:__EMAIL__,X-Auth-Key:__APIKEY__" http-data="{\"type\":\"A\",\"name\":\"__DOMAIN__\",\"content\":\"__IPADDRESS__\",\"ttl\":120,\"proxied\":false}" output=none

//working
curl -X POST "https://api.cloudflare.com/client/v4/zones/__ZONEID__/dns_records" -H "X-Auth-Email: __EMAIL__" -H "X-Auth-Key: __APIKEY__" -H "Content-Type: application/json" --data '{"type":"A","name":"__DOMAIN__","content":"__IPADDRESS__","ttl":120,"priority":10,"proxied":false}'
Anyone else have the same problem?
 
dejoebad
just joined
Posts: 16
Joined: Mon Jul 21, 2014 5:05 pm

Re: POST Request with fetch

Wed Mar 25, 2020 4:40 pm

Multiple headers already possible for a few versions, even documented in Wiki:
https://wiki.mikrotik.com/wiki/Manual:T ... Properties

It's also discussed in this topic:
viewtopic.php?f=9&t=120860&p=709811&hil ... rs#p717584
with fetch, how can I do curl command like this:
curl -X POST -d @sw_debug_mode.xml http://192.168.8.1/CGI

please assist me, i'm very new with this✌☺
 
tempoff
just joined
Posts: 4
Joined: Fri May 10, 2019 1:47 pm

Re: POST Request with fetch

Mon Mar 30, 2020 1:02 pm

Hello everyone!

Can you also add a patch http method to fetch tool please? It can be very useful.
Guys? :(
 
alkaafi
just joined
Posts: 1
Joined: Tue Jun 02, 2020 2:43 am

Re: POST Request with fetch

Tue Jun 02, 2020 3:24 am

can help me?
<form id="loginForm" action="https://welcome2.wifi.id/wms/auth/authnew/autologin/quarantine.php?ipc=10.212.63.176&gw_id=WAG-D5-KBL&mac=14:4d:67:98:69:9c&redirect=&wlan=BWRGJ00021-N%2FTLK-CI-47595%3Aelkafii" method="post">
                            <input type="text" name="username" 	id="username_hidden" 	value="2078orla.Ai03@freeMS" />
                            <input type="text" name="password"	id="password" 		value="2078orla" />
<input type="submit" value="oke" />
</form>
for auto login

	{:local wmsip "10.212.63.176";
	:local wmsmac "14:4d:67:98:69:9c";
	:local wmspass "2078orla";
	:local wmsssid "BWRGJ00021-N%2FTLK-CI-47595%3Aelkafii";
	:local urllogin "https://welcome2.wifi.id/wms/auth/authnew/autologin/quarantine.php\?ipc=$wmsip&gw_id=WAG-D5-KBL&mac=$wmsmac&redirect=&wlan=$wmsssid";
	:local ppostform "ipc=$wmsip&gw_id=WAG-D5-KBL&mac=$wmsmac&redirect=&wlan=$wmsssid&payload={\"username\":\"$wmspass.Ai03@freeMS\",\"password\":\"$wmspass\"}";
	/log err $ppostform
	/ip dns cache flush
	:local wmsrespon ([/tool fetch mode=https http-method=post http-data=$ppostform url="$urllogin" http-header-field="content-type:application/json,User-Agent: chrome browser mumet" as-value output=user]->"data");
	/log war $wmsrespon;}
please
 
User avatar
iyadseizer
just joined
Posts: 2
Joined: Mon Nov 06, 2017 1:35 am
Location: syria
Contact:

Re: POST Request with fetch

Sun Aug 16, 2020 4:58 am

hello all
that is easy way to fetch your data
in
http-header-field
[important]
[flash=]example[/flash]
http-header-field="Authorization:key=Izalg..gf,Content-Type:application/json"
and in body you will write a json template as this
http-data="{\"to\":\"deviceToken\",\"notification\":{\"title\":\"hello\",\"body\":\"world\"}}"
that is a full way i used at firebase notification from my server to my android app
regurds
Image
 
knedlyk
just joined
Posts: 10
Joined: Sat Jul 18, 2020 4:18 pm

Re: POST Request with fetch

Wed Dec 22, 2021 6:55 am

Hi! Please, help to convert this string into the fetch format:
curl -d '{"token": "<roomid>", "message": "hi there @<username>"}' -H "Content-Type: application/json" -H "Accept: application/json" -H "OCS-APIRequest: true" -v -u "<username>:<password>" https://mydomain.tld/ocs/v1.php/apps/spreed/api/v1/chat/<roomid>

Let me add that https://kigiri.github.io/fetch/ gives me the following solution:
fetch("https://domaintld/ocs/v1.php/apps/spreed/api/v1/chat/<roomid>", {
  body: "{"token": "<roomid>", "message": "hi there @<username>"}",
  headers: {
    Accept: "application/json",
    Authorization: "Basic <hash here=>",
    "Content-Type": "application/json",
    "Ocs-Apirequest": "true"
  },
  method: "POST"
})

I tried to convert it to
/tool fetch http-method=post http-header-field="{Accept:\"appli
cation/json\",Authorization:\"Basic <hash here=>\",\"Content-Type\":\"appl
ication/json\",\"OCS-APIRequest\":\"true\"}" http-data="{\"token\":\"<roomid>\",\"message\":\"hi t
here @<username>\"}" url="domain.tld/ocs/v1.php/apps/spreed/api/v1/chat/<roomid>"
 user="<username>" password="<password>"


However, got the response:
status: failed
failure: closing connection: <412 Precondition failed> 144.**.**.***:443 (5)

Playing with adding or not adding user and password didn't help.

Please, help with that. Thanks!
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: POST Request with fetch

Wed Dec 22, 2021 4:04 pm

Hi! Please, help to convert this string into the fetch format:
curl -d '{"token": "<roomid>", "message": "hi there @<username>"}' -H "Content-Type: application/json" -H "Accept: application/json" -H "OCS-APIRequest: true" -v -u "<username>:<password>" https://mydomain.tld/ocs/v1.php/apps/spreed/api/v1/chat/<roomid>
/tool/fetch follows RouterOS usual scheme for commands and types, so a direct conversation isn't possible... While shares same name as the W3C fetch(), it actually pretty different in operation. The curl example is closer to what's needed.

For references... Mikrotik's old docs describe the parameters better: https://wiki.mikrotik.com/wiki/Manual:Tools/Fetch
The new docs have better examples: https://help.mikrotik.com/docs/display/ ... ch-Summary
The HTTP example for IoT with KNOT is more fuller example of using /tool/fetch.


So focusing on the CURL -> /tool/fetch pthings
I tried to convert it to
/tool fetch http-method=post http-header-field="{Accept:\"appli
cation/json\",Authorization:\"Basic <hash here=>\",\"Content-Type\":\"appl
ication/json\",\"OCS-APIRequest\":\"true\"}" http-data="{\"token\":\"<roomid>\",\"message\":\"hi t
here @<username>\"}" url="domain.tld/ocs/v1.php/apps/spreed/api/v1/chat/<roomid>"
 user="<username>" password="<password>"


However, got the response:
status: failed
failure: closing connection: <412 Precondition failed> 144.**.**.***:443 (5)

Mikrotik doesn't have native JSON support, so it's generally not used in most parameters to /tool/fetch. While it's need for the "http-data" – since JSON is what the other side wants, ROS allows you to send a encode string with it as you found out. e.g. the "{\"field\":\"value\"}" stuff is JSON and needed in http-data ONLY. Mikrotik requires you escape the JSON (specifically JSON's required quotes around attribute names) which look like you've done.

But "http-header-field=<...>" does not take JSON (but W3C fetch() does, why the confusion I'm sure) – so your escaping/formating of JSON the into this field is the issue. In HTTP, the actual key:value headers pars don't use JSON, they use a : colon. The old docs document the format, so use HTTP's colon format here. With Mirotik telling you to separate out multiple headers by a ",". So in theory, the fix for your case is to use:
http-header-field="Content-Type:application/json,OCS-APIRequest:true,Accept:application/json"
You may need the "Authentication" line, but think that will get set automatically by RouterOS when you use user= password= (can't recall). And yeah perhaps escaping. But that the header line is issue I suspect.
 
knedlyk
just joined
Posts: 10
Joined: Sat Jul 18, 2020 4:18 pm

Re: POST Request with fetch

Tue Dec 28, 2021 3:34 am


You may need the "Authentication" line, but think that will get set automatically by RouterOS when you use user= password= (can't recall). And yeah perhaps escaping. But that the header line is issue I suspect.
Many-many-many thanks for your so kind guidance. It works now!
What I would like to improve is to use the anonymous posts in public conversations, however it brings additional pain: need to use 2 curls and analyse cookies. I have had a conversation with the Talk's devs, and they left not so easy to take ideas https://github.com/nextcloud/spreed/issues/6736.

In case of MT I wouldn't like to store the cookies on the device side, but rather try to place them in vars. In any case, do you think it will be possible to obtain them at all?

Thanks again!
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: POST Request with fetch

Tue Dec 28, 2021 3:56 am


You may need the "Authentication" line, but think that will get set automatically by RouterOS when you use user= password= (can't recall). And yeah perhaps escaping. But that the header line is issue I suspect.
What I would like to improve is to use the anonymous posts in public conversations, however it brings additional pain: need to use 2 curls and analyse cookies. I have had a conversation with the Talk's devs, and they left not so easy to take ideas https://github.com/nextcloud/spreed/issues/6736.
You might want to start a new topic in the forum, explaining what you're planning/doing with /tool/fetch & nextcloud. This thread is now 3+ years old, so new subtopics get really confusing. Plus, you may not be only person using a Mikrotik to Nextcloud. They seems to have a few possible APIs...maybe someone has a script already.
 
knedlyk
just joined
Posts: 10
Joined: Sat Jul 18, 2020 4:18 pm

Re: POST Request with fetch

Sat Jan 01, 2022 2:53 pm



What I would like to improve is to use the anonymous posts in public conversations, however it brings additional pain: need to use 2 curls and analyse cookies. I have had a conversation with the Talk's devs, and they left not so easy to take ideas https://github.com/nextcloud/spreed/issues/6736.
You might want to start a new topic in the forum, explaining what you're planning/doing with /tool/fetch & nextcloud. This thread is now 3+ years old, so new subtopics get really confusing. Plus, you may not be only person using a Mikrotik to Nextcloud. They seems to have a few possible APIs...maybe someone has a script already.
Thanks! I will probably follow your advice. Have a good start of the New year! 🎇
 
austinNz
just joined
Posts: 6
Joined: Tue Sep 21, 2021 7:54 pm
Contact:

Re: POST Request with fetch

Sun Aug 13, 2023 7:37 am

Late reply but for anyone searching for this now like me I found out it is much easier to send out the http-data as an array
:local arr {"mac"="aa:bb:cc:dd:ee:ff";"ip"="192.168.100.80"}; /tool fetch url=("https://some-server.com/index.php") http-method=https http-data=$arr mode=post keep-result=yes;
In the php file I can read the post array as $_POST["mac"] and $_POST["ip"]
 
stevedavis
just joined
Posts: 3
Joined: Thu Nov 22, 2018 11:32 am

Re: POST Request with fetch

Thu Nov 16, 2023 12:52 pm

Does anyone know if it is possible to do something similar to POST but using a plain Telnet like data stream rather than HTTP application specific?

I just need to send text to a port at an address , a bit like you can manually send commands to an email server or http server from a Telnet client.

Thanks

Who is online

Users browsing this forum: No registered users and 19 guests