/tool fetch - to variable

Hello, I have this script:

:local IP "10.111.0.22"
:local name "Senzi"

:local file "sdcard/json-$name.txt"
/tool fetch url="http://$IP/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\":\"Player.GetActivePlayers\",\"id\":1}" mode=http dst-path=$file
:delay 1
:local json [/file get $file contents]
:if ([:find $json "video"] + 0 = 0) do={
  /log info "TV $name play"
  /tool fetch url="http://$IP/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\":\"Player.Open\",\"params\":{\"item\":{\"playlistid\":0},\"options\":{\"repeat\":\"all\"}},\"id\":1}" mode=http keep-result=no
}
/file remove $file

It is working, but I have more than one script (different IPs and names) and two things bother me:

  • each time a file is created - it is using flash or sdcard
  • log is full of “info - fetch: file … downloaded”

I would like to run script each few minutes.

So - is there a way to improve the script? It would be very nice if the file will not be necessary - is it possible to fetch to variable?

I have a similar problem

I want to read the result of a fetch being 1 or 0
and if 1 then do something if 0 do nothing

but I always have to write to the flash to read it back unfortunately

we should be able to do something like
:local answer [/tools fetch url=“http://example.com” keep-result=no]

any solutions would be appreciated

+1 useful on api source site

Same here, that would be handy.

My workaround is to send current state with url parameters, then if there is nothing to change the HTTP server answers with status code 400. This raises an error at mikrotik side and aborts the script without writing any file or log entry.

This solve is not so good as can be. “Error/not error” is like an “0” or “1” answeer :slight_smile:. But if I want to send a data to mikrotik (some updates of parameters) as an answeer of a get-request - I can’t use this solution. In a 6.42 firmware was a good news: “*) fetch - added “output” option for all modes in order to return result to file, variable or ignore it;” May be it is what we all looking for? Does anybody use this option (“output”)? Official Wiki don’t give any instructions about that. How we can use “output” parameter?

It still doesn’t work. Below is the answer from support@mikrotik.com
Would bi nice to have it but …

Hello,
At the moment you can not read this value in variable yet. Unfortunately we have not managed to finalise this new feature and it still requires an improvements.
Please follow our changelog and we will update it when additional feature to fetch tool will be added that allows to read received data.

Best regards,
Martins S.

Since RouterOS 6.43 it s possible to fetch to variable, no file is needed:

:local IP "10.111.0.21"
:local name "Povazie"

/tool fetch url="http://$IP/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\":\"Input.Select\",\"id\":1}" mode=http keep-result=no
:local json [/tool fetch url="http://$IP/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\":\"Player.GetActivePlayers\",\"id\":1}" mode=http as-value output=user]
:if ([:find ($json->"data") "video"] + 0 = 0) do={
  /log info "TV $name play"
  /tool fetch url="http://$IP/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\":\"Player.Open\",\"params\":{\"item\":{\"directory\":\"/home/osmc/Movies\"},\"options\":{\"repeat\":\"all\"}},\"id\":1}" mode=http keep-result=no
}

I don’t understand what output=user means, but it is necessary.

It means that fetch output will be sent to user variable.