OPNsense
  • Home
  • Help
  • Search
  • Login
  • Register

  • OPNsense Forum »
  • English Forums »
  • Development and Code Review (Moderator: fabian) »
  • [SOLVED] Add route via api
« previous next »
  • Print
Pages: [1]

Author Topic: [SOLVED] Add route via api  (Read 4744 times)

FriendOfCarlotta

  • Newbie
  • *
  • Posts: 13
  • Karma: 1
    • View Profile
[SOLVED] Add route via api
« on: May 18, 2018, 12:57:23 pm »
Hello!

I try to add a new route via the api using Python 3 and requests:

Code: [Select]
apikey = 'xxx'
apisecret = 'xxx'
url = 'https://10.0.0.254/api/routes/routes/addroute'
requests.post(url, data=payload, verify=None, auth=(apikey, apisecret))

payload is
Code: [Select]
{'route': {'network': '10.0.50.0/24', 'gateway': {'Null4': {'value': 'Null4 - 127.0.0.1', 'selected': 0}, 'Null6': {'value': 'Null6 - ::1', 'selected': 0}, 'VLAN_GW': {'value': 'VLAN_GW - 10.0.0.253', 'selected': 1}, 'WAN_DHCP': {'value': 'WAN_DHCP - 192.168.107.2', 'selected': 0}}, 'descr': 'vlan50', 'disabled': '0'}}

As result I receive:
Code: [Select]
{"errorMessage":"Error at /usr/local/opnsense/mvc/app/models/OPNsense/Routes/Route.php:59 - Undefined index:  (errno=8)"}

On the other side I am able to change an existing route via
Code: [Select]
/api/routes/routes/setroute/<uuid> with the same payload.

The payload has been received with
Code: [Select]
getroute and was modified accordingly.

Any hints are appreciated.

Regards, Thomas
« Last Edit: May 19, 2018, 10:06:13 pm by FriendOfCarlotta »
Logged

fvanroie

  • Newbie
  • *
  • Posts: 20
  • Karma: 10
    • View Profile
    • PS_OPNsense
Re: Add route via api
« Reply #1 on: May 18, 2018, 06:12:02 pm »
Just change the gateway field to the name you want as a string. Also the JSON keys/values should be in double quotes:

Code: [Select]
payload = '{"route": {"network": "10.0.50.0/24", "gateway": "LAN_DHCP", "descr": "vlan50", "disabled": "0"}}'

200 OK
{ "result": "saved" }
« Last Edit: May 18, 2018, 06:15:32 pm by fvanroie »
Logged

FriendOfCarlotta

  • Newbie
  • *
  • Posts: 13
  • Karma: 1
    • View Profile
Re: Add route via api
« Reply #2 on: May 18, 2018, 07:29:11 pm »
Thanks for the reply.

In fact I tried this syntax already. But I only get
Code: [Select]
<Response [200]> as feedback and the route does not show up in the ui.  :-\

How do I get a more verbose response?
Logged

fabian

  • Moderator
  • Hero Member
  • *****
  • Posts: 2768
  • Karma: 199
  • OPNsense Contributor (Language, VPN, Proxy, etc.)
    • View Profile
    • Personal Homepage
Re: Add route via api
« Reply #3 on: May 18, 2018, 07:41:25 pm »
The API is not verbose and always returns 200 (except when an exception is thrown). You have to check the response body for the JSON result.
Logged

FriendOfCarlotta

  • Newbie
  • *
  • Posts: 13
  • Karma: 1
    • View Profile
Re: Add route via api
« Reply #4 on: May 18, 2018, 08:00:46 pm »
Ok, thanks so far. I'll check this after the holiday.  8)
Logged

fvanroie

  • Newbie
  • *
  • Posts: 20
  • Karma: 10
    • View Profile
    • PS_OPNsense
Re: Add route via api
« Reply #5 on: May 18, 2018, 08:28:50 pm »
Quote from: FriendOfCarlotta on May 18, 2018, 07:29:11 pm
How do I get a more verbose response?

This works for me:
Code: [Select]
#/usr/bin/python

# import libraries
import json
import requests

# define endpoint and credentials
api_key = ''
api_secret = ''

url = 'https://127.0.0.1/api/routes/routes/addroute'
headers = {'content-type': 'application/json'}
payload = '{"route": {"network": "10.0.50.0/24", "gateway": "LAN_DHCP", "descr": "vlan50", "disabled": "0"}}'

# Make the api call
r = requests.post(url, data=payload, verify=False, auth=(api_key, api_secret), headers=headers)

# Check response
if r.status_code == 200:
    print r.text
    response = json.loads(r.text)

    if response['result'] == 'saved':
        print ('The payload was successfully saved !')
        print (response['result'])

else:
    print ('Connection / Authentication issue, response received:')
    print r.text

Logged

FriendOfCarlotta

  • Newbie
  • *
  • Posts: 13
  • Karma: 1
    • View Profile
Re: Add route via api
« Reply #6 on: May 19, 2018, 10:05:11 pm »
Thanx! The headers parameter does the trick. :-)
Logged

  • Print
Pages: [1]
« previous next »
  • OPNsense Forum »
  • English Forums »
  • Development and Code Review (Moderator: fabian) »
  • [SOLVED] Add route via api
 

OPNsense is an OSS project © Deciso B.V. 2015 - 2023 All rights reserved
  • SMF 2.0.19 | SMF © 2021, Simple Machines
    Privacy Policy
    | XHTML | RSS | WAP2