Community discussions

MikroTik App
 
aleab
Member Candidate
Member Candidate
Topic Author
Posts: 110
Joined: Sat Sep 22, 2018 6:13 pm

count-only in rest api

Mon May 08, 2023 11:21 pm

Hello,
i try to check with rest api total wifi clients.
for now i'm testing on my HAP ax2 with ROS 7.9.
so on mikrotik i use
/interface/wifiwave2/registration-table/print count-only where ssid="WIFI GUEST"
but with rest api i try to use
curl -k -u admin:12345678 -X POST https://10.2.1.1:10443/rest/interface/w ... able/print
i see correct all wifi client (on all ssid)
curl -k -u admin:12345678 -X POST https://10.2.1.1:10443/rest/interface/w ... able/print --data '{".query": ["ssid=WIFI GUEST"]}' -H "content-type: application/json"
to try now i use m laptop windows 11, and i have this error
{"detail":"Invalid JSON","error":400,"message":"Bad Request"}curl: (3) bad range in URL position 2:
[ssid=WIFI GUEST]}'
^
and i can't found documentation to use command "count-only"
can you help me?

thank you
 
Spidermila
just joined
Posts: 9
Joined: Wed May 19, 2021 12:37 pm

Re: count-only in rest api

Thu Aug 24, 2023 2:01 pm

I couldn't reproduce your exact problem. My below query works as expected:
curl -k -u test:test -X POST https://192.168.1.1/rest/caps-man/registration-table/print --data '{".query": ["ssid=myssid"]}' -H "content-type: application/json"
It returns the same results as this console command:
/caps-man/registration-table/print where ssid="myssid"
I suspect your problem could be caused by the space in SSID. Try changing your SSID so it doesn't contain spaces to confirm.
Hope this helps.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: count-only in rest api

Thu Aug 24, 2023 2:08 pm

try to remove the space on SSID name... or add quotes on query :lol:
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: count-only in rest api

Thu Aug 24, 2023 4:04 pm

POST requires knowing Mikrotik "binary" API...

So the issue is that .query with POST is follows same rules as API, so the end of the .query array describes the and/or/etc. See https://help.mikrotik.com/docs/display/ ... PI-Queries

And the "attributes without options" – like print "count-only", need to be key-values booleans in the REST POST e.g. { ..., "count-only": true }
curl -l -u $myuser -X POST http://$myrouter/rest/ip/address/print --data '{".query": ["disabled=yes","#|"], "count-only": true}'
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: count-only in rest api

Thu Aug 24, 2023 4:35 pm

or add quotes on query :lol:
I think quotes will actually break it... It's already in it's own element so the "=" is separator for attributes, and next array element is next thing, so spaces shouldn't be an issue.
 
aleab
Member Candidate
Member Candidate
Topic Author
Posts: 110
Joined: Sat Sep 22, 2018 6:13 pm

Re: count-only in rest api

Thu Sep 21, 2023 5:15 pm

sorry sorry sorry!!!!
i'm trying from win pc, and i have error below...
but on linux works fine... :D :D :D


sorry, i understand i change my ssid without spaces...

but now i would ty to monitor netwatch and i try to
curl -k -u admin:12345678 -X POST https://10.2.5.1:10443/rest/tool/netwatch/print --data '{".query": ["host=10.2.5.200"]}' -H "content-type: application/json"
but this is error
{"error":415,"message":"Unsupported Media Type"}curl: (3) bad range in URL position 2:
[host=10.2.5.200]}'
 ^
but here there isn't spaces...
i thinked to use comment, but there is a lot of spaces, so i used host

instead
curl -k -u admin:12345678 -X POST https://10.2.5.1:10443/rest/tool/netwatch/print
works fine

can you have a suggestion ?
thank you in advance
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: count-only in rest api

Thu Sep 21, 2023 5:43 pm

You first example should work, but doesn't for me either.

But using the ".../getall", instead of ".../print" in URL does allow the ".query" string to work. The REST API's POST methods really are just thin proxy over the "old" API, so all of this stuff largely applies to REST with POST: https://help.mikrotik.com/docs/display/ ... escription
getall
getall command is available where console print command is available (getall is an alias for print).
replies contain =.id=Item internal number property.

To me it's bug that "print with .query" doesn't work... but I think the /getall will avoid. e.g. if it's "alias for print" then it should do same thing, but here it doesn't...
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: count-only in rest api

Thu Sep 21, 2023 5:51 pm

Sorry I think your problem is just putting a single quote around the URL & I'd use "Content-Type", not just lowercased version, just to be safe.

curl -k -u admin:12345678 -X POST 'https://10.2.5.1:10443/rest/tool/netwatch/print' --data '{".query": ["host=10.2.5.200"]}' -H "Content-Type: application/json"
 
aleab
Member Candidate
Member Candidate
Topic Author
Posts: 110
Joined: Sat Sep 22, 2018 6:13 pm

Re: count-only in rest api

Thu Sep 21, 2023 9:21 pm

sorry for delay.

i confirm in linux ( almalinux 8.8 ) with curl
curl 7.61.1 (x86_64-redhat-linux-gnu) libcurl/7.61.1 OpenSSL/1.1.1k zlib/1.2.11 brotli/1.0.6 libidn2/2.2.0 libpsl/0.20.2 (+libidn2/2.2.0) libssh/0.9.6/openssl/zlib nghttp2/1.33.0
works fine all commands...
curl -k -u admin:12345678 -X POST https://x.x.x.x:10443/rest/tool/netwatch/print | jq .
and also
curl -k -u admin:12345678 -X POST https://x.x.x.x:10443/rest/tool/netwatch/getall | jq .
same result

works also
curl -k -u admin:12345678 -X POST https://x.x.x.x:10443/rest/tool/netwatch/print --data '{".query": ["host=1.1.1.1"]}' -H "content-type: application/json" | jq . | grep status
and also
curl -k -u admin:12345678 -X POST https://x.x.x.x:10443/rest/interface/wifiwave2/registration-table/print --data '{".query": ["ssid=WIFI GUEST"], "count-only": true}' -H "content-type: application/json" | jq .
of course you can omit | jq .

i try with hap ac2 (ROS 7.11.2) and hap ax2 (ROS 7.11.2)
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: count-only in rest api

Thu Sep 21, 2023 11:48 pm

So it's working now?

I really would be careful with the quotes on windows. " " isn't ‟ ” — you'll note the subtle "smart quotes"....

And I use a single quotes ' ' around the URL on windows too: 'http.../netwatch/print' — windows paths are weird.

Can't say either are your problem, but those are the two I know to watch for on Windows.
 
aleab
Member Candidate
Member Candidate
Topic Author
Posts: 110
Joined: Sat Sep 22, 2018 6:13 pm

Re: count-only in rest api

Fri Sep 22, 2023 8:41 am

yes, for comfort when i approach api i used windows,
but my goal is use on linux because i want to use with zabbix server (so on linux).

practically my problem is solved because i use linux.
but if someone want to use on windows still have problem... i think is quote / double quote / ecc...

Who is online

Users browsing this forum: No registered users and 27 guests