Mikrotik RouterOS HTTP request to OH

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\"}" 

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:

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.

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

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”

1 Like

Hi, can you please post working script?

Hi, this is my work script

:log info "start"
:global sendItem;
:global sendItemState;

:log info  $sendItem

/tool fetch mode=http url="http://192.168.1.6:8080/rest/items/$sendItem" http-header-field="content-type: text/plain" http-data="$sendItemState"
:log info "finish"

@Tema Thank you