I asked the same question a few months ago - only one reply confirming the response code is not available. Essential functionality in my view and a pity it isn’t implemented.
Since RouterOS v6.43 it is possible to save the result of fetch command to a variable. For example, it is possible to trigger a certain action based on the result that a HTTP page returns. You can find a very simple example below that disables ether2 whenever a PHP page returns “0”:
This seems to only work when the fetch result is successful.
If we try another example with a wrong URL (correct = reqbin.com), the result is only the error, not the failed status.
{
:local result [/tool fetch url=https://reqb.com/echo/get/json output=user as-value]
:if ($result->"status" = "failed") do={
:put ($result->"status")
# Empty status
}
}
So, inside a script, it would be nice to treat the error with a friendlier message.
When a fetch fails, how can I capture the status and error message?
If the problem is a “wrong URL”… It’s either the DNS could not be resolve OR TCP cannot connect to the IP resolved. AFAIK, nothing will tell you that specifically, just “failed” as status (although you can check DNS using “:resolve” before).
But if HTTP is not successful at connecting, there really is not some “status code” as those come AFTER the server connects.
They added “output=user-with-headers” option recently, but that doesn’t help on DNS failure.
Also, if there is a some text about the failure in the error message (and depending on issue, there is). You might be able to simplify @rextended’s approach using the newer “:onerror” in very recent V7: