While reading the documentation I saw nothing that would describe why this would not work, hence my request that the documentation be updated to reflect the ways in which :put will not do what is intuitive (and how print statements work in other scripting languages).
RouterOS script is "consitently, inconsistent" for sure. Likely some arguable bug here.
But not sure the problem with with the ":put", rather the string that it's
trying to print. If string interpolation (aka "substitution") results in an error, which is what happens
sometimes with -
:put "value=$val" - if $val is an array... then, the string can fail and if it does nothing is returned . Since the string substitution happens before the :put, when the "stack unwinds" to the :put, it's an empty string. Basically it's best to need to make sure the variables your substituting are string, even an empty one, otherwise they
may (or may not) "fail" with depending on exactly what type (num, array, ip, ip6, ip-prefix, etc., etc.), and string substitution fails, the
entire string is null.
So I think the bigger issue is the response from /tool/fetch is an "array" type, and string interpolation is, well, "spotty" with arrays. Why/when, not sure - best not to use a variable array as a variable in a string. To fix your issue, you likely can just wrap it in a [:tostr $response] like so:
:put "debug: response=$([:tostr $response])"
I think the extra "$([...])" around the variable creates a call on the stack, thus another "failure scope", so in the above if an error happens, the stack "unwinds" to the $([...]), so only that part be empty, not an error. In theory, ROS script should be smart enough to attempt the :tostr "cast" itself, and it may... but the $([...]) syntax acts like a "catch", at least in my observations. The :tostr part is needed since that will return an "empty string" not an error/nil/nothing.
Basically there is a lot undocumented, but the bigger issue is MT is "sloppy" in their variable types e.g. some CLI command will cast automatically, others won't; and some conversion aren't possible without hacks.