Community discussions

MikroTik App
 
User avatar
robyhr
newbie
Topic Author
Posts: 25
Joined: Fri May 25, 2007 8:03 pm
Location: Croatia
Contact:

Fetch, JSON and authentication-types

Thu Jan 09, 2020 6:38 pm

I have a problem with fetch command when outputing JSON data that contains wlan authentication-types field. Here is example:

Script 1
:local wifissid [/interface wireless get [ find default-name=wlan1 ] ssid]
/tool fetch http-method=post http-header-field="content-type:application/json" http-data="{\"wifissid\":\"$wifissid\"}" url="http://domain.name/index.php"
Output 1
{"wifissid":"WiFiNetwork123"}

Script 2
:local wifiauthtype [/interface wireless security-profiles get [ find default=yes ] authentication-types]
/tool fetch http-method=post http-header-field="content-type:application/json" http-data="{\"wifiauthtype\":\"$wifiauthtype\"}" url="http://domain.name/index.php"
Output 2
{"wifiauthtype":"wpa-psk"};{"wifiauthtype":"wpa2-psk"}

Second output is not valid JSON file. Is there a way to fix this problem? I think it has to do with parsing field authentication-types that contains , or ;
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Fetch, JSON and authentication-types

Fri Jan 10, 2020 11:35 am

Your problem is that "authentication-types" returns an array. How is your JSON supposed to look?
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Fetch, JSON and authentication-types  [SOLVED]

Fri Jan 10, 2020 11:51 am

If output "{"wifiauthtype":"wpa-psk;wpa2-psk"}" is ok... Try this:
:local wifiauthtype [ :tostr [ /interface wireless security-profiles get [ find default=yes ] authentication-types ] ]
/tool fetch http-method=post http-header-field="content-type:application/json" http-data="{\"wifiauthtype\":\"$wifiauthtype\"}" url="http://domain.name/index.php"
 
User avatar
robyhr
newbie
Topic Author
Posts: 25
Joined: Fri May 25, 2007 8:03 pm
Location: Croatia
Contact:

Re: Fetch, JSON and authentication-types

Fri Jan 10, 2020 12:43 pm

Thank you very much, that solved it. I also found explanation here: https://wiki.mikrotik.com/wiki/Manual:S ... _to_string

Who is online

Users browsing this forum: alexantao, maxslug, rjuho and 23 guests