Community discussions

MikroTik App
 
syadnom
Forum Veteran
Forum Veteran
Topic Author
Posts: 802
Joined: Thu Jan 27, 2011 7:29 am

v7 API address-list http methods driving me nuts...

Mon Oct 16, 2023 5:17 am

Hi all. I'm trying to do a couple things all on address lists via API.

I cannot pull the existing address lists via get and http://ip/rest/ip/firewall/address-list

I see the post meth but I can't find the right json body entry to get it to reply on rest/ip/firewall/address-list/print

My intension is to pull the existing address list, match it against some other data, and the add/update/delete entries.

I'm using postman for interactive queries and my processing flow is in n8n.

Thanks for any advice on this.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3509
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: v7 API address-list http methods driving me nuts...

Mon Oct 16, 2023 4:05 pm

To get the list, you use EITHER:
-X GET http://ip/rest/ip/firewall/address-list
or
-X POST http://ip/rest/ip/firewall/address-list/print
... You can't use /print with the GET.

Similar to add a new address-list entry, that's also use PUT or POST:
-X PUT http://ip/rest/ip/firewall/address-list --json '{"address": "127.0.0.42", "list": "mylist"}'
or
-X POST http://ip/rest/ip/firewall/address-list/add --json '{"address": "127.0.0.42", "list": "mylist"}'
... Again POST uses the CLI keyword "add" while PUT infers that from HTTP method.

If you're using postman, I did create a schema for it here: viewtopic.php?p=1024965 – not perfect but helps.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3509
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: v7 API address-list http methods driving me nuts...

Mon Oct 16, 2023 4:45 pm

I'm using postman for interactive queries and my processing flow is in n8n.
I should add that when using POST operations, the JSON can have a root of "ret:", so if your looking for an array from the results in n8n you may need to add a .ret
 
syadnom
Forum Veteran
Forum Veteran
Topic Author
Posts: 802
Joined: Thu Jan 27, 2011 7:29 am

Re: v7 API address-list http methods driving me nuts...

Mon Oct 16, 2023 6:33 pm

To get the list, you use EITHER:
-X GET http://ip/rest/ip/firewall/address-list
or
-X POST http://ip/rest/ip/firewall/address-list/print
... You can't use /print with the GET.

Similar to add a new address-list entry, that's also use PUT or POST:
-X PUT http://ip/rest/ip/firewall/address-list --json '{"address": "127.0.0.42", "list": "mylist"}'
or
-X POST http://ip/rest/ip/firewall/address-list/add --json '{"address": "127.0.0.42", "list": "mylist"}'
... Again POST uses the CLI keyword "add" while PUT infers that from HTTP method.

If you're using postman, I did create a schema for it here: viewtopic.php?p=1024965 – not perfect but helps.
Very strange, but I opened up postman this morning and hit run and what didn't work yesterday (get address-list), works today. Everything works, postman and n8n's http requester.

Thanks.
 
syadnom
Forum Veteran
Forum Veteran
Topic Author
Posts: 802
Joined: Thu Jan 27, 2011 7:29 am

Re: v7 API address-list http methods driving me nuts...

Mon Oct 16, 2023 7:38 pm

ok, next step, how to remove? I'm not sure how to handle the 'where' for a remove.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3509
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: v7 API address-list http methods driving me nuts...

Mon Oct 16, 2023 8:14 pm

Well, that's a more complex story. It may be helpful to think the REST API has TWO different APIs. One uses POST, which is an HTTP-proxy around the "binary" API https://help.mikrotik.com/docs/display/ROS/API ... so for a HTTP-based REST POST request, the "where" manifests itself as { ".query": ["list=myaddrlist"] } & support the RPN/stack-based filter in the JSON array but using "=" in the string based API-syntax – tricky business....

On a GET call, the "where" is more simple & just part of the URL in the query string. So on a GET ?list=myaddlist
And similar with PATCH where the .id (e.g. what shows as * in CLI print show-ids) is part of the path so PATCH (or "set"):
 -X PATCH http://router.lan/rest/ip/firewall/address-list/*5 --json '{ "address": "myhostname" }'
PUT is same as "add", so the data is entirely in the JSON). The GET/PUT/PATCH reflects the "2nd" REST API — these are the "real" REST APIs ;).

So in the 2nd method, you do a GET to know the .id, then a PUT using .id from GET as part of the path to "set".

For examples/explaination, see these:
viewtopic.php?t=198974
viewtopic.php?t=196021
 
BharayaTasayar
just joined
Posts: 2
Joined: Sun Jun 04, 2023 7:03 am

Re: v7 API address-list http methods driving me nuts...

Tue Oct 17, 2023 10:04 am

To pull the existing address lists via the Microtik API, you can use the following GET request:

GET /rest/ip/firewall/address-list
This request will return a list of all address lists on the router.

To match the existing address list against some other data, you can use the filter parameter. For example, the following request will return all address lists with a name that contains the string "my_address_list":

GET /rest/ip/firewall/address-list?filter=name~my_address_list
To add, update, or delete entries in an address list, you can use the POST request for /rest/ip/firewall/address-list/print. The JSON body for this request should contain the following parameters:

name: The name of the address list.
add: A list of entries to add to the address list.
remove: A list of entries to remove from the address list.
set: A list of entries to update in the address list.
For example, the following request will add a new entry to the address list my_address_list:

POST /rest/ip/firewall/address-list/print
{
"name": "my_address_list",
"add": [
{
"address": "192.168.1.100/24",
"comment": "This is a new entry."
}
]
}
You can use Postman for interactive queries and n8n to create a processing flow that automates your address list management tasks.

Here is an example of a n8n flow that pulls the existing address lists from the Microtik API and then matches them against some other data:

Start --> HTTP Request (GET) --> Finish

HTTP Request (GET)
URL: http://ip/rest/ip/firewall/address-list
Method: GET
Headers:
Authorization: Bearer YOUR_API_TOKEN

Finish
Condition:
Expression: {{data.length > 0}}
Then:
- Set Data:
Variable: addressLists
Value: {{data}}
Else:
- Throw Error:
Message: No address lists found.
You can then add additional nodes to the flow to match the address lists against some other data and to add, update, or delete entries in the address lists.

I hope this helps!
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3509
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: v7 API address-list http methods driving me nuts...

Tue Oct 17, 2023 8:16 pm

I'm not sure JSON with "add" is valid on POST to ...address-list/print
For example, the following request will add a new entry to the address list my_address_list:

POST /rest/ip/firewall/address-list/print
{
"name": "my_address_list",
"add": [
{
"address": "192.168.1.100/24",
"comment": "This is a new entry."
}
]
}

Who is online

Users browsing this forum: No registered users and 6 guests