fetch - how receive response code

Hello!
I ask you to help with getting an answer when requesting a http site.
When you request a site using curl:

C:\Users>curl example.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Found</TITLE></HEAD><BODY>
<H1>302 Found</H1>
The document has moved
<A HREF="http://zero.2090000.ru">here</A>
</BODY></HTML>

Show headers:

C:\Users>curl -s -I example.com
HTTP/1.1 302 Found
Content-Length: 219
Location: http://zero.2090000.ru
Content-Type: text/html; charset=UTF-8

in the answer you need to find http://zero.2090000.ru and execute script1, if not, then execute script2.

When I request through mikrotik, I get an error and the inability to pass to a variable (I don’t know how to do this), what would I process further.

[user@mtgw] > :global value [/tool fetch url=http://example.com/ output=user]          
  status: failed

failure: closing connection: <302 Found "http://zero.2090000.ru"> 193.232.46.60:80 (4)

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.

J

Have a look at my script which is uses the result.

http://forum.mikrotik.com/t/address-lists-downloader-dshield-spamhaus-drop-edrop-etc/133640/33

From the Wiki:
Return value to a variable

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”:

{
    :local result [/tool fetch url=http://10.0.0.1/disable_ether2.php as-value output=user];
    :if ($result->"status" = "finished") do={
        :if ($result->"data" = "0") do={
            /interface ethernet set ether2 disabled=yes;
        } else={
            /interface ethernet set ether2 disabled=no;
        }
    }
}

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?

http://forum.mikrotik.com/t/fetch-capable-of-following-redirects/151723/7

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.

That’s a shame.
This seems to be a very old user need.
I guess the best option is to resort to files as @rextended suggested.

I hope MT improves the Fetch tool one day. :crossed_fingers:

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:

:onerror errtext in={/tool/fetch/...} do={ :put $errtext; :pick [ :find $errtext ...] ... }

But… you do have to know the error text your looking & nothing prevent Mikrotik from changing the message. So this way is problematic too.