OPNsense Forum

English Forums => Development and Code Review => Topic started by: bubbagump on March 19, 2021, 01:39:34 PM

Title: Understanding API docs
Post by: bubbagump on March 19, 2021, 01:39:34 PM
I want to automate a few items to toggle routes and and other simple things via the API. The first item I am trying to do is toggle enable/disable an alias. (Toggle alias on, a certain host in the alias matches a conditional route and routes out gateway B. Toggle off, it routes out gateway A.)

I have a user and API access is working. For instance, I ran

curl -k -u "sometoken":"somekey" https://192.168.40.2/api/firewall/alias/getAliasUUID/SomeAlias
and voila I got back

{"uuid":"44724741-c37e-419a-9fa2-3aad79111335"}

Fantastic. Auth works.

Now I am completely lost on making the POST. I tried this based on the POST example here https://docs.opnsense.org/development/how-tos/api.html (https://docs.opnsense.org/development/how-tos/api.html).

curl -XPOST -d '{"ed8a5bf8-861a-4307-a7b4-df1b2a727d18":"null"}' -H "Content-Type: application/json" -k -u "sometoken":"somekey" https://192.168.40.2/api/firewall/alias/toggleItem

and I get

{"message":"action toggleItem expects at least 1 parameter(s)","status":400}

which I half expect as I have no clue what values to put in. The docs say Parameter $uuid,$enabled=null. Great, I have a uuid, what values are allowed for $enabled? I have tried true/false, 1/0. Is there a table somewhere that describes these values? Or am I so green there is some convention here that a real dev would understand that I don't? Thanks!
Title: Re: Understanding API docs
Post by: mimugmail on March 19, 2021, 02:19:41 PM
I didn't work with Alias yet, but I'd suggest you type F12 in browser and do this by hand, then you'll see the API calls sent via browser
Title: Re: Understanding API docs
Post by: bubbagump on March 19, 2021, 03:57:43 PM
Ooooo, that's clever. Thank you!
Title: Re: Understanding API docs
Post by: bubbagump on March 19, 2021, 04:19:24 PM
Here we go...

curl -k -u "sometoken":"somekey" 'https://192.168.40.2/api/firewall/alias/toggleItem/44724741-c37e-419a-9fa2-3aad79111335'  --data-raw '{}'

"$enabled=null" in the docs simply mean to pass a null set in the --data-raw - so hopefully this helps someone somewhere.

Title: Re: Understanding API docs
Post by: mimugmail on March 19, 2021, 06:00:13 PM
Thx for sharing