Support Forum
The Forums are a place to find answers on a range of Fortinet products from peers and product experts.
PurpleShirt
New Contributor III

Error 424 when trying to add an IP address via REST API

Hi everyone

 

I am trying to use the REST API to add IP addresses on our Firewall. 

 

I have this python code: 

 

import os
import requests
import urllib3
import getpass

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

baseURL = "REDACTED URL"
apiURL = "/api/v2/cmdb/firewall/address"
token = "REDACTED TOKEN"

headers = {'Authorization':'Bearer ' + token,}

params = {
   "datasource":1,
   "vdom": "root"
   }

def createNewIpAddressData(ipAddress):
   data = {
      "name":"ext-" + ipAddress,
      "subnet":ipAddress + "/32",
      "comment": "New IP",
      "color":"0"
   }
   return data

def addNewIpAddress(postData):
    try:
      r = requests.post(baseURL + apiURL, headers=headers, params=params, verify=False, data=postData, timeout=5)
    except requests.exceptions.Timeout:
      print("Timeout!")
      return None
    
    if r.status_code != 200:
       print("Error code:{}".format(r.status_code))
       print(r.content)
       return None
    
    return r.json

result = addNewIpAddress(createNewIpAddressData("5.153.183.99"))
print (str(result))

 

When I execute this, I receive this error with the htpp_status 424: 

 

 "http_method":"POST",\n  "revision":"REDACTED",\n  "revision_changed":false,\n  "status":"error",\n  "http_status":424,\n  "vdom":"root",\n  "path":"firewall",\n  "name":"address",\n  "serial":"REDACTED",\n  "version":"v7.0.12",\n  "build":523\n

 

 I've used the API code from my FortiGate and according to the documentation, it says that it misses some dependencies, but I can't figure out what it misses. 

 

Can someone point me in the right direction? 

 

Thanks. 

1 Solution
PurpleShirt
New Contributor III

Ok I found the issue. I had to encode it to json before passing it to the post request. 

 

Here the updated code, if someone needs it. 

 

import os
import requests
import urllib3
import getpass
import json

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

baseURL = "REDACTED"
apiURL = "/api/v2/cmdb/firewall/address"
token = "REDACTED"

headers = {'Authorization':'Bearer ' + token,}

params = {
   "datasource":1,
   "vdom": "root"
   }

def createNewIpAddressData(ipAddress):
   data = [{
      "name":"ext-" + ipAddress,
      "subnet":ipAddress + "/32",
      "comment": "New IP",
      "color":"0"
   }]
   jsondata = json.dumps(data)
   return jsondata

def addNewIpAddress(postData):
    try:
      r = requests.post(baseURL + apiURL, headers=headers, params=params, verify=False, data=postData, timeout=5)
    except requests.exceptions.Timeout:
      print("Timeout!")
      return None
    
    if r.status_code != 200:
       print("Error code:{}".format(r.status_code))
       print(r.content)
       return None
    
    return r.json

result = addNewIpAddress(createNewIpAddressData("5.153.183.99"))
print (str(result))

 

View solution in original post

1 REPLY 1
PurpleShirt
New Contributor III

Ok I found the issue. I had to encode it to json before passing it to the post request. 

 

Here the updated code, if someone needs it. 

 

import os
import requests
import urllib3
import getpass
import json

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

baseURL = "REDACTED"
apiURL = "/api/v2/cmdb/firewall/address"
token = "REDACTED"

headers = {'Authorization':'Bearer ' + token,}

params = {
   "datasource":1,
   "vdom": "root"
   }

def createNewIpAddressData(ipAddress):
   data = [{
      "name":"ext-" + ipAddress,
      "subnet":ipAddress + "/32",
      "comment": "New IP",
      "color":"0"
   }]
   jsondata = json.dumps(data)
   return jsondata

def addNewIpAddress(postData):
    try:
      r = requests.post(baseURL + apiURL, headers=headers, params=params, verify=False, data=postData, timeout=5)
    except requests.exceptions.Timeout:
      print("Timeout!")
      return None
    
    if r.status_code != 200:
       print("Error code:{}".format(r.status_code))
       print(r.content)
       return None
    
    return r.json

result = addNewIpAddress(createNewIpAddressData("5.153.183.99"))
print (str(result))

 

Announcements

Select Forum Responses to become Knowledge Articles!

Select the “Nominate to Knowledge Base” button to recommend a forum post to become a knowledge article.

Labels
Top Kudoed Authors