Hi,
my use-case is relatively simple, I would like to toggle a (LAN-) rule via API.
Based on https://docs.opnsense.org/development/how-tos/api.html#id4, I'm able to connect - but as soon as I try to use the toggle_rule endpoint (https://docs.opnsense.org/development/api/core/firewall.html#id6) I receive the following error;
{'result': 'failed'}
This is the endpoint I'm using (<RULE-UUID> based on the corresponding entry in the config.xml);
https://<OPNSENSE>/api/firewall/filter/toggle_rule/<RULE-UUID>
The request itself;
api_response = requests.post(opnsense_api_url, auth=(opnsense_api_key, opnsense_api_secret), verify=False)
Can you please tell me what I'm missing? Thanks for your time!
AIUI It needs to be sent as a POST request, with uuid as part of the json payload.
Hi,
tried it with and without payload, same effect.
api_response = requests.post(opnsense_api_url, auth=(opnsense_api_key, opnsense_api_secret), verify=False)
has the same effect as
json_payload = { 'uuid': <UUID> }
api_response = requests.post(opnsense_api_url, auth=(opnsense_api_key, opnsense_api_secret), verify=False, json=json_payload)
I also tried several different "command" nodes (toggle_rule, toggleRule), together with /1 at the end and without.. always the same result ({'result': 'failed'})
https://<OPNSENSE>/api/firewall/filter/toggle_rule/<UUID>/1
https://<OPNSENSE>/api/firewall/filter/toggleRule/<UUID>/1
https://<OPNSENSE>/api/firewall/filter/toggle_rule/<UUID>
https://<OPNSENSE>/api/firewall/filter/toggleRule/<UUID>
Reading further at https://docs.opnsense.org/development/api/core/firewall.html#id6... "Rules not visible in the web interface (Firewall ‣ Automation) will not be returned by the API either." -> I don't have such a menu entry on 26.1.1. Does that mean that API endpoint isn't working for "standard" (non-automation) rules?
Correct. Only "new rules" will be visible over the API.
That did the trick! I've not migrated my "old rules" to "new rules", so I created a simple test "new rule".
Running my Python-script works like a charm, so
many thanks again for taking your time and responding!If anyone is interested;
- as meyergru stated: API access to toggle_rule only works for "new rules", created via https://<OPNSENSE>/ui/firewall/filter/, as only those are exposed via the API
- You don't need a (json-)payload for the POST request, the API endpoint is https://<OPNSENSE>/api/firewall/filter/toggle_rule/<UUID>
- The POST request is as simple as api_response = requests.post(opnsense_api_url, auth=(opnsense_api_key, opnsense_api_secret), verify=False)