01-30-2023 04:17 AM
Hi,
New to API's and coding in general.
I am trying to write a script that adds a new firewall rule to a network (I'm using a test site atm).
However I am getting a 404 error which I assume means my URL is wrong?
I got this from the Meraki reference guide but I'm not sure its correct.
/networks/{network_id}/appliance/firewall/l3firewallRules
Also - I read somewhere that adding a new rule would overwrite all the existing rules! Is that true and if so is there a way to add a rule so that it doesn't effect any existing rules?
Thanks!
import requests
import security
import json
api_key = security.MERAKI_API_KEY
organizationId = security.ORG_ID
network_id = "xxxxxxxxx"
rule = {
'name': 'Test Rule',
'policy': 'deny',
'protocol': 'any',
'srcPort': 'any',
'srcCidr': '1.1.1.1/24',
'dstPort': 'any',
'dstCidr': '0.0.0.0/0',
}
url = f"https://api.meraki.com/api/v1/networks/{network_id}/appliance/firewall/l3firewallRules"
headers = {
'X-Cisco-Meraki-API-Key': api_key,
'Content-Type': 'application/json'
}
response = requests.post(url, headers=headers, data=json.dumps(rule), verify=False)
print(response.status_code)
Solved! Go to Solution.
01-30-2023 05:38 AM
HTTP400 is sent because the dashboard doesn't like your request. Doesn't mean you are missing a parameter.
Using the Meraki SDK would be easier to troubleshoot.
The issue is within the payload.
data=json.dumps(rule)
01-30-2023 04:27 AM
Here is the correct URL:
https://developer.cisco.com/meraki/api/#!update-network-l-3-firewall-rules
01-30-2023 05:14 AM
Hi,
Thanks for the reply, but Im still getting a 404 error 😞
am I getting the url wrong or can something else cause this issue?
url = f"https://api.meraki.com/api/v1/networks/{network_id}/l3FirewallRules"
response = requests.post(url, headers=headers, data=json.dumps(rule), verify=False)
01-30-2023 05:21 AM
404 - Not Found The requested resource doesn't exist or incorrect API key
You can test It on the Meraki developer Hub before.
01-30-2023 05:21 AM
I would troubleshoot this issue with these steps :
1- Create a dummy rule from the dashboard
2- GET the rule via the API
3- PUT the exact same response received from step #2.
You have to be Org admin / Net admin to do that.
01-30-2023 05:33 AM
huh - if I try to use the PUT operation from the dashboard - its gets a 404 there too
01-30-2023 05:36 AM
Have you enabled API access?
01-30-2023 05:43 AM
Yes, as mentioned, moments before i tried the PUT I did a successful GET
01-30-2023 05:19 AM
I think your link may be the out of date version 0
Version 1
https://developer.cisco.com/meraki/api-v1/#!update-network-appliance-firewall-l-3-firewall-rules
01-30-2023 05:22 AM
Use this one:
https://developer.cisco.com/meraki/api-latest/
01-30-2023 05:30 AM
If I run the exact same script as a GET - it works ok. I can also run the get from the dashboard.
I noticed I was using POST rather than PUT so made that change but if I do that I get a 400 error instead 😞
url = f"https://api.meraki.com/api/v1/networks/{network_id}/appliance/firewall/l3FirewallRules"
response = requests.put(url, headers=headers, data=json.dumps(rule), verify=False)
01-30-2023 05:33 AM
400 - Bad Request The request was unacceptable, often due to missing a required parameter.
01-30-2023 05:35 AM
shouldn't the dashboard tell me if there are any required parameters?
01-30-2023 05:38 AM
HTTP400 is sent because the dashboard doesn't like your request. Doesn't mean you are missing a parameter.
Using the Meraki SDK would be easier to troubleshoot.
The issue is within the payload.
data=json.dumps(rule)
01-30-2023 05:41 AM
Not pretty , but should work.
import os
import json
import requests
import codecs
base_url_v1 = 'https://api.meraki.com/api/v1'
NetworkID = XXXXXXXXXXX
headers = {
'x-cisco-meraki-api-key': format(str(apikey)),
'Content-Type': 'application/json'
}
def __returnhandler(statuscode, returntext):
if str(statuscode) == '200':
return returntext
else:
print('HTTP Status Code: {0}\n'.format(statuscode))
def getL3FirewallRules():
geturl = '{0}/networks/{1}/appliance/firewall/l3FirewallRules'.format(str(base_url_v1), str(NetworkID))
dashboard = requests.get(geturl, headers=headers,verify=False)
result = __returnhandler(dashboard.status_code, dashboard.text)
return result
L3FWRules = getL3FirewallRules()
payload = L3FWRules
url = 'https://api.meraki.com/api/v1/networks/{0}/appliance/firewall/l3FirewallRules'.format(NetworkID)
response = requests.request('PUT', url, headers=headers, data = payload,verify=False)
print(response.text.encode('utf8'))
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide