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

TNSR API payload requirements

Scheduled Pinned Locked Moved TNSR
5 Posts 2 Posters 1.9k Views
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 Apr 7, 2022, 10:53 AM Apr 7, 2022, 10:50 AM

    This post is deleted!
    1 Reply Last reply Reply Quote 0
    • F
      fuzzysteve
      last edited by Apr 7, 2022, 10:57 AM

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

      F 1 Reply Last reply Apr 7, 2022, 11:22 AM Reply Quote 0
      • F
        fuzzysteve @fuzzysteve
        last edited by Apr 7, 2022, 11:22 AM

        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
        • J
          jimp Rebel Alliance Developer Netgate
          last edited by Apr 7, 2022, 1:46 PM

          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 Apr 8, 2022, 11:08 AM Reply Quote 0
          • F
            fuzzysteve @jimp
            last edited by Apr 8, 2022, 11:08 AM

            @jimp Thanks!

            1 Reply Last reply Reply Quote 0
            5 out of 5
            • First post
              5/5
              Last post
            Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.
              This community forum collects and processes your personal information.
              consent.not_received