Search code examples
pythonapisonicwall

How to post an object to sonicwall api with python?


recently my admin asked me if I can create an app to input some data into our Sonicwall. I found some API, created an account, and ran some "get" methods, which worked just fine. I am trying now to post a new object into our pv4 rules and cannot get over it. The problem is when I write something like this:

def postIpv4Object(session):
body = {
    "address_objects": [{
            "ipv4": {
                "name": "Test 1",
                "zone": "LAN",
                "host": {
                    "ip": "192.168.168.10"
                }
            }
        }
    ]
}

resp = session.post(fw + '/api/sonicos/address-objects/ipv4', headers=good_headers,params=body, verify=False)

I am still getting this error:

{'status': {'info': [{'code': 'E_INVALID_API_CALL',
                  'level': 'error',
                  'message': 'Expected a request body.'}],
        'success': False}}

I am reading docs but cannot really figure this out. Does anyone tried it and help me out a little?


Solution

  • After a couple of days of trying, I figured that "body" should be converted into JSON type, and instead of "params I need to use "data".