Community discussions

MikroTik App
 
TerAnYu
newbie
Topic Author
Posts: 28
Joined: Sun Jun 20, 2010 1:46 pm
Location: Russia, Novosibirsk

fetch - how receive response code

Fri Mar 20, 2020 11:06 am

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)
 
stejjh
just joined
Posts: 6
Joined: Sat Apr 07, 2018 6:16 pm

Re: fetch - how receive response code

Sat Mar 28, 2020 9:07 pm

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
 
msatter
Forum Guru
Forum Guru
Posts: 2912
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: fetch - how receive response code

Sat Mar 28, 2020 9:41 pm

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

viewtopic.php?f=9&t=152632&p=778113&hilit=fetch#p759427
 
msatter
Forum Guru
Forum Guru
Posts: 2912
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: fetch - how receive response code

Sat Mar 28, 2020 9:45 pm

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;
        }
    }
}
 
diasdm
newbie
Posts: 30
Joined: Fri Sep 22, 2023 4:48 pm

Re: fetch - how receive response code

Sun Mar 24, 2024 4:11 am

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?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: fetch - how receive response code

Sun Mar 24, 2024 1:04 pm

 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3505
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: fetch - how receive response code

Sun Mar 24, 2024 2:58 pm

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.
 
diasdm
newbie
Posts: 30
Joined: Fri Sep 22, 2023 4:48 pm

Re: fetch - how receive response code

Sun Mar 24, 2024 8:45 pm

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. 🤞
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3505
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: fetch - how receive response code

Sun Mar 24, 2024 9:18 pm

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. 🤞
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.

Who is online

Users browsing this forum: Bing [Bot] and 7 guests