Bad request: There was a problem in the JSON you submitted error - Code Help

SOLVED
Austin_Campbell
Getting noticed

Bad request: There was a problem in the JSON you submitted error - Code Help

I am creating a script for mass l7 firewall changes and I am running into an error. My code is below. I have made sure that my layer7rules.json file is properly formatted in json. 

 

import requests
import json
import os
from dotenv import load_dotenv

load_dotenv()
api_key = os.environ.get("API_Key")

with open("layer7rules.json") as f:
payload = json.load(f)

with open("testnetids.txt") as file:
array = file.readlines()
for line in array:
line = line.rstrip("\n")
url = "https://api.meraki.com/api/v0/networks/%s/l7FirewallRules" % line
headers = {
'Content-Type': 'application/json',
'X-Cisco-Meraki-API-Key': api_key
}

response = requests.request("PUT", url, headers=headers, data=payload)

print(response.text)


 

1 ACCEPTED SOLUTION

Thank you for your suggestion. I was able to figure out the issue and the code is working. I had to add data=json.dumps(payload) because json.load() strips the double quotations required for json when converting the file to a python dictionary.  

View solution in original post

14 REPLIES 14
Inderdeep
Kind of a big deal
Kind of a big deal

@Austin_Campbell : I think the below code from your script should be in double quotes. What error message you are getting ?

 

headers = {
'Content-Type': 'application/json',
'X-Cisco-Meraki-API-Key': api_key
}

 

Regards/Inder
Cisco IT Blogs awarded in 2020 & 2021
www.thenetworkdna.com

I am getting the error "Bad request: There was a problem in the JSON you submitted". Tried with double quotes same result. Here is my json file.

 

{
"rules": [
{
"policy": "deny",
"type": "blacklistedCountries",
"value": [
"CN",
"CU",
"HK",
"ID",
"IR",
"LY",
"LT",
"MY",
"MX",
"NE",
"KP",
"PK",
"RU",
"SC",
"KR",
"SY",
"TW",
"TH",
"TR",
"UA",
"AE",
"YE"
]
},
{
"policy": "deny",
"type": "ipRange",
"value": "80.82.77.139/32"
},
{
"policy": "deny",
"type": "ipRange",
"value": "149.202.120.37/32"
},
{
"policy": "deny",
"type": "ipRange",
"value": "208.91.197.27/32"
},
{
"policy": "deny",
"type": "ipRange",
"value": "217.174.152.68/32"
},
{
"policy": "deny",
"type": "ipRange",
"value": "193.56.28.125/32"
},
{
"policy": "deny",
"type": "ipRange",
"value": "92.242.140.21/32"
},
{
"policy": "deny",
"type": "ipRange",
"value": "185.165.190.34/32"
},
{
"policy": "deny",
"type": "ipRange",
"value": "223.71.167.163/32"
}
]
}

@Austin_Campbell : Your Jason seems correct, Check and validate below your script.

https://jsonlint.com/

 

Regards/Inder
Cisco IT Blogs awarded in 2020 & 2021
www.thenetworkdna.com

Thanks for the link. I tested and it does pass as valid json. 

and also Replace the base URL with the actual shard you are on. Google script does not seem to follow the redirect. E.g. https://n248.meraki.com/api.... instead of https://api.meraki.com ... More info about that issue here: 

https://documentation.meraki.com/General_Administration/Other_Topics/Cisco_Meraki_Dashboard_API#Stat... 

Regards/Inder
Cisco IT Blogs awarded in 2020 & 2021
www.thenetworkdna.com

No luck with this change either. I believe this to be a specific problem with the python script and the way I am loading the json file into the api call.

Can you use Post method ?

Regards/Inder
Cisco IT Blogs awarded in 2020 & 2021
www.thenetworkdna.com

No.

Inderdeep
Kind of a big deal
Kind of a big deal

@Austin_Campbell : Change the URL as well fron v0 to v1 as well.

url = "https://api.meraki.com/api/v0/networks

 

Regards/Inder
Cisco IT Blogs awarded in 2020 & 2021
www.thenetworkdna.com

I have tried this as well no luck with v0 or v1 same error. 

PhilipDAth
Kind of a big deal
Kind of a big deal

Try setting up one entry exactly how you would like in the dashboard, then 'get' those settings and look at the json return.  In fact, you should be able to use that json for your future put calls.

The json file that I have was produced from a postman meraki api "get" of our standard l7 rules. I am able to use a json.dump of the exact json I have above and it works successfully for a "put".

Edgar-VO
Building a reputation

Hi,

 

Much easier is it like this :

 

def Create_L7_Firewall(def_network):
 
    if new_class_type == "C":
        try:
            with open('Vessel-C-SDtraffic-L7.json') as my_file:
                my_SD_traffic = json.load(my_file)
        except:
            print ("File Vessel-C-SDtraffic-L7.json not found")
    elif new_class_type == "B":
        try:
            with open('C:\\Scripts\\Create BC Vessel\\Vessel-B-SDtraffic-L7.json') as my_file:
                my_SD_traffic = json.load(my_file)
        except:
            print ("File Vessel-B-SDtraffic-L7.json not found")
 
    response = dashboard.appliance.updateNetworkApplianceFirewallL7FirewallRules(def_network,**my_SD_traffic)

 

We have Class B and C vessels..... each Class has a different set of rules...

 

Just read the json file and push it into the network

 

Best regards

Ed

 

Thank you for your suggestion. I was able to figure out the issue and the code is working. I had to add data=json.dumps(payload) because json.load() strips the double quotations required for json when converting the file to a python dictionary.  

Get notified when there are additional replies to this discussion.