Mikrotik RouterOS HTTP request to OH

mikrotik
routeros
Tags: #<Tag:0x00007f51dc6890e0> #<Tag:0x00007f51dc688f78>

(martiniman) #1

Hi.
Help with PUT HTTP request from Mikrotik router. I want to get MACs of wifi users from router to openHAB using this script.
New version of RouterOS is support any requests: https://forum.mikrotik.com/viewtopic.php?t=120860#p594370
My knowledge in RouterOS is tiny.

I need to update item via REST API like this:

curl -X PUT --header "Content-Type: text/plain" --header "Accept: application/json" -d "Test" "http://192.168.1.11:8080/rest/items/Test_Item/state"

I tried, but no luck:

/tool fetch mode=http url="http://192.168.1.11:8080/rest/items/Test_Item/state"  http-method=put content-type=application/json http-data="payload={ \"Test\"}" 

(Kevin) #3

Paste your complete Script from RouterOS , then it is possible to find the Error.

Only doing this in RouterOS shell will not be enough.

Edit:

I think there is an understanding problem. Where do you run your script? In OpenHab or in RouterOS?

You have to do it in RouterOS, because there is the program fetch.

PUT is available to get things from other systems.
POST is available to send things to other systems.

I think you need POST :wink:


(martiniman) #4

This script runs every minute on Mikrotik router.

:global mac "88:88:88:6E:74:49"
:global OldChek
:global chek
if ([/interface wireless registration-table find mac-address=$mac] != "") do={set chek true} else={set chek false}
if (($chek != $OldChek) and ($chek = true)) do={
/log warning  ($mac . " -ON")

**** this line do not work ********
/tool fetch mode=http url="http://192.168.1.11:8080/rest/items/Test_Item/state"  http-method=put content-type="text/plain" http-data="payload={ \"ON\"}" 

}

if (($chek != $OldChek) and ($chek = false)) do={
/log warning  ($mac . " -OFF")

**** this line do not work ********
/tool fetch mode=http url="http://192.168.1.11:8080/rest/items/Test_Item/state"  http-method=put content-type="text/plain" http-data="payload={ \"OFF\"}" 

}
set OldChek $chek

Script is work ok.
PUT - to update item state.
POST - to send command.


Mikrotik Binding idea
(Kevin) #5

Yes you’re right, I was probably tired when I wrote the answer … and I messed up, get, put and post.


(Артем Бондаренко) #6

To owerload content type need use directive http-header-field and set key content-type in lower case, example
http-header-field=“content-type:application/json”