Community discussions

MikroTik App
 
colin
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 90
Joined: Mon May 11, 2015 11:11 am

How to exclude content when query by rest api

Sat Aug 26, 2023 11:42 am

Hi guys, i want to query some nat rules like this:
/ip/firewall/nat/print where chain !="srcnat" and chain !="dstnat" 
How to do this query by rest api? Thanks.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4143
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: How to exclude content when query by rest api

Sat Aug 26, 2023 5:13 pm

In the REST API, you need to use POST operation to do that. The ".query" parameter will control the filter string & that's the hard part. Essentially .query I think needs to need with "#!", but you'll have to play around.

Someone else just ask something similar, so this pretty similar to what your trying to do:
viewtopic.php?t=196021&hilit=post+rest#p1021063
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4143
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: How to exclude content when query by rest api

Sat Aug 26, 2023 6:23 pm

Specifically, the syntax for the ".query" is use a stack (like an HP / RPN calculator), unlike "find"/"where" in script...
/ip/firewall/nat/print where chain !="srcnat" and chain !="dstnat" 
How to do this query by rest api? Thanks.

Using curl something like this should work:

USER=admin ROUTER=192.168.88.1; curl -l -u $USER -X POST http://$ROUTER/rest/ip/firewall/nat/print -H "Content-Type: application/json" --data '{".query": ["chain=dstnat","chain=srcnat","#|!"]}' 

So it's "#|!" that's needed, e.g. first "OR" the chain= ones (top two items in stack), then apply the "NOT" (!) using the result of the first operation. See https://help.mikrotik.com/docs/display/ ... PI-Queries for the gory details of how .query works, same for REST's POST as "old"/non-REST API
 
colin
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 90
Joined: Mon May 11, 2015 11:11 am

Re: How to exclude content when query by rest api

Sat Aug 26, 2023 7:27 pm

Specifically, the syntax for the ".query" is use a stack (like an HP / RPN calculator), unlike "find"/"where" in script...
/ip/firewall/nat/print where chain !="srcnat" and chain !="dstnat" 
How to do this query by rest api? Thanks.

Using curl something like this should work:

USER=admin ROUTER=192.168.88.1; curl -l -u $USER -X POST http://$ROUTER/rest/ip/firewall/nat/print -H "Content-Type: application/json" --data '{".query": ["chain=dstnat","chain=srcnat","#|!"]}' 

So it's "#|!" that's needed, e.g. first "OR" the chain= ones (top two items in stack), then apply the "NOT" (!) using the result of the first operation. See https://help.mikrotik.com/docs/display/ ... PI-Queries for the gory details of how .query works, same for REST's POST as "old"/non-REST API
Hi @Amm0:
Thank you for your help. I understand now.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4143
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: How to exclude content when query by rest api

Sat Aug 26, 2023 7:31 pm

Yeah basically REST's "POST" is just a wrapper around the older API. So anything that works there, is just mutated into JSON/etc.

Who is online

Users browsing this forum: GoogleOther [Bot], Kunci, pajapatak, rmj and 30 guests