Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    TNSR API payload requirements

    TNSR
    2
    5
    1.8k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • F
      fuzzysteve
      last edited by fuzzysteve

      This post is deleted!
      1 Reply Last reply Reply Quote 0
      • F
        fuzzysteve
        last edited by

        I'm dumb. (don't have it working yet)
        Should have paid more attention to the docs.

        F 1 Reply Last reply Reply Quote 0
        • F
          fuzzysteve @fuzzysteve
          last edited by

          ok, with better formatted json, I'm now getting a different error.

          Failed to find YANG spec of XML node: static with parent: static in namespace: urn:netgate:xml:yang:netgate-nat

          the sample code I've put together to do it. I'm getting the feeling it's probably something simple that I'm missing.

          payload=dict()
          payload['netgate-nat:static']=dict()
          payload['netgate-nat:static']['mapping-table']=dict()
          payload['netgate-nat:static']['mapping-table']['mapping-entry']=[]
          
          baseport=8732
          ipfirst="192.168.49."
          externaladdress="myexternalip"
          for octet in range(2,3):
              entry=dict()
              entry["local-port"]="3425"
              entry["route-table-name"]="ipv4-VRF:0"
              entry["transport-protocol"]="tcp"
              entry["local-address"]="{}{}".format(ipfirst,octet)
              entry["external-port"]="{}".format(octet+baseport)
              entry["external-address"]=externaladdress
              payload['netgate-nat:static']['mapping-table']['mapping-entry'].append(entry)
          
          
          
          import json
          import requests
          
          from requests.auth import HTTPBasicAuth
          basic = HTTPBasicAuth('username', 'password')
          
          headers = {'Content-type': 'application/yang-data+json'}
          
          url = "http://10.79.79.49/restconf/data/netgate-nat:nat-config/netgate-nat:static"
          
          response = requests.post(url,json=payload,auth=basic,headers=headers)
          print(json.dumps(payload))
          print(response.text)
          

          payload is

          {"netgate-nat:static": {"mapping-table": {"mapping-entry": [{"local-port": "3425", "route-table-name": "ipv4-VRF:0", "transport-protocol": "tcp", "local-address": "192.168.49.2", "external-port": "8734", "external-address": "myexternalip"}]}}}
          
          1 Reply Last reply Reply Quote 0
          • jimpJ
            jimp Rebel Alliance Developer Netgate
            last edited by

            The trick is making sure the payload and URL agree in that the payload should be contained within the URL's namespace.

            This works, for example:

            $ curl -k --fail-with-body \
               --cert ~/tnsr/tnsr-myuser.crt \
               --key ~/tnsr/tnsr-myuser.key \
               --cacert ~/tnsr/tnsr-selfca.crt \
               -H "Content-Type: application/yang-data+json" \
               -d '{"netgate-nat:mapping-entry": [{"transport-protocol": "tcp", "local-address": "10.2.0.5", "local-port": "22", "external-address": "203.0.113.2", "external-port": "222", "route-table-name": "ipv4-VRF:0" }]}' \
               -X POST \
               https://198.51.100.2/restconf/data/netgate-nat:nat-config/netgate-nat:static/netgate-nat:mapping-table/
            

            You also have to be careful to scope it correctly. Doing a POST at a higher level than that would create the entire static mapping table (and likewise a PUT would replace it), not individual entries.

            You want to be looking at this bit in the docs:

            https://docs.netgate.com/tnsr/en/latest/api/netgate-nat.html#tag/nat-config/paths/~1data~1netgate-nat:nat-config~1netgate-nat:static~1netgate-nat:mapping-table/post

            Whereas your example was a couple levels too high.

            Remember: Upvote with the 👍 button for any user/post you find to be helpful, informative, or deserving of recognition!

            Need help fast? Netgate Global Support!

            Do not Chat/PM for help!

            F 1 Reply Last reply Reply Quote 0
            • F
              fuzzysteve @jimp
              last edited by

              @jimp Thanks!

              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.