Community discussions

MikroTik App
 
User avatar
BrianHiggins
Forum Veteran
Forum Veteran
Topic Author
Posts: 702
Joined: Mon Jan 16, 2006 6:07 am
Location: Norwalk, CT
Contact:

Using fetch to get an array 😣

Sat Nov 05, 2022 12:51 am

I've been banging my head against the wall for the past ~5 hours and have gotten absolutely no-where on this. Can anyone show a method that works? testing on 6.49.7...

I am trying to use fetch to pull down the contents of an array, and then retrieve elements of it for later use. It looks like it should work, but the best I can tell it's not recognizing the data in an array format and treating it like a single string, and thus no element data gets returned.
:local strData {[:toarray ([/tool fetch mode=https output=user url="https://myserver.com/getvalues" as-value ]->"data")]}
This server returns the following string (which I can modify formatting if needed to solve this, and I tried it with and without the {} and found no difference)
{ssid="network";username="test@domain";pass="mypass123"}
This much appears fine, but the problem is when I try to retrieve any element of this array I get a blank output.
:local strData {[:toarray ([/tool fetch mode=https output=user url="https://myserver.com/getvalues" as-value ]->"data")]}
:put $strData
:put "The Password is $($strData->"pass")"

[admin@RouterOS] /system script> run test 
ssid="network";username="test@domain";pass="mypass123"
The Password is
However when I run the same thing off data that I populate directly from the script it works
:local strData {ssid="network";username="testuser";pass="mypass123"}
:put "The Password is $($strData->"pass")"

[admin@RouterOS] /system script> run test 
The Password is mypass123
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Using fetch to get an array 😣

Sat Nov 05, 2022 8:48 am

Obviously any read from file, is a string.
Obviously :toarray can convert only string to simple index array ( 1,2,3,4,5,6 )
Obviously if you pass a string that is not one array, but script syntax ( {ssid="network";....;pass="mypass123"} ), this do not convert syntax into array...
Obviously is incorrect syntax :local strData {[ ... ]}
If url is provided, mode is useless

Readed from file, or declared as string, for this example is equal:
{
# :local strData ([/tool fetch url="https://myserver.com/getvalues" output=user as-value ]->"data")
:local strData "{ssid=\"network\";username=\"test@domain\";pass=\"mypass123\"}"
:local arrData [[:parse ":local temp $strData; :return \$temp"]]
:put "The Password is $($arrData->"pass")"
}
 
User avatar
BrianHiggins
Forum Veteran
Forum Veteran
Topic Author
Posts: 702
Joined: Mon Jan 16, 2006 6:07 am
Location: Norwalk, CT
Contact:

Re: Using fetch to get an array 😣

Sat Nov 05, 2022 4:29 pm

Readed from file, or declared as string, for this example is equal:
{
# :local strData ([/tool fetch url="https://myserver.com/getvalues" output=user as-value ]->"data")
:local strData "{ssid=\"network\";username=\"test@domain\";pass=\"mypass123\"}"
:local arrData [[:parse ":local temp $strData; :return \$temp"]]
:put "The Password is $($arrData->"pass")"
}
As posted that had a small typo, but I fixed the unescaped " in front of $strData and that code doesn't work even running on the local string. I also tried it on the downloaded data using both escaped \" and straight " with no difference....
:local strData "{ssid=\"network\";username=\"test@domain\";pass=\"mypass123\"}"
:put $strData
:local arrData [[:parse ":local temp \$strData; :return \$temp"]]
:put "The Password is $($arrData->"pass")"
:put "The Network is $($arrData->"ssid")"
:put [:typeof $arrData]
:put [:len $arrData]

:foreach val in=$arrData  do={
:put "Items: $val"
}
[admin@RouterOS] /system script> run test 
{ssid="network";username="test@domain";pass="mypass123"}
The Password is 
The Network is 
nil
0
[admin@RouterOS] /system script> 
 
User avatar
BrianHiggins
Forum Veteran
Forum Veteran
Topic Author
Posts: 702
Joined: Mon Jan 16, 2006 6:07 am
Location: Norwalk, CT
Contact:

Re: Using fetch to get an array 😣

Sat Nov 05, 2022 4:41 pm

ok, I don't know why it worked this time, but I swear I pasted the original code and it didn't work (and why I assumed that $strData needed escaped, because it stopped throwing an error on that line when I did)

but now it works!
This line is the key to making it work.
:local arrData [[:parse ":local temp $strData; :return \$temp"]]
:put "The Password is $($arrData->"pass")"
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Using fetch to get an array 😣

Sun Nov 06, 2022 7:15 pm

Didn't you read what I wrote?
You already had the solution in front of your nose...
 
User avatar
BrianHiggins
Forum Veteran
Forum Veteran
Topic Author
Posts: 702
Joined: Mon Jan 16, 2006 6:07 am
Location: Norwalk, CT
Contact:

Re: Using fetch to get an array 😣

Fri Nov 11, 2022 7:31 am

Didn't you read what I wrote?
yup, and already posted about it just above your post... don't think you read the reply :wink:

Who is online

Users browsing this forum: Cmon169, Google [Bot] and 14 guests