400 Bad request: There was a problem in the JSON you submitted

Joe_Bennett
Here to help

400 Bad request: There was a problem in the JSON you submitted

I've written a Python script to add static routes but I'm running into a JSON error. My original code was messy but worked, appending text to a string until the HTML body was complete

string="{"
string=string + '"name": "' + network["Name"] + '",'
string=string + '"subnet": "' + network["Network"] + '",'
string=string + '"gatewayIp":"' + gateway["GW"] + '",'
string=string + '"enabled":"true"}'

I then tried to use

 

payload = {
"name" : network["Name"],
"subnet": network["Network"],
"gatewayIp": gateway["GW"],
"enabled":"true"
}

which doesn't work

The output is as follows, the first line is rejected, the second works fine.

{'name': '10.2.1.0-24', 'subnet': '10.2.1.0/24', 'gatewayIp': '192.168.129.5', 'enabled': 'true'}
{"name": "10.2.1.0-24","subnet": "10.2.1.0/24","gatewayIp":"192.168.129.5","enabled":"true"}

The only difference appears to be the quote type. Is the API this picky? and is there a way to use the second code stanza to produce valid code? It works for the requests.request header, but not the body.

 

Thanks,

Joe

3 REPLIES 3
PhilipDAth
Kind of a big deal
Kind of a big deal

Whenever I look at json it uses the double quote " - not the single quote.  Is the single quote even valid syntax?

The JSON structure requires double quotes. This is not specific to the Python language, but a standard with the JSON format.

 

 

I figured it out.

 

import json

payload = {
"name" : network["Name"],
"subnet": network["Network"],
"gatewayIp": gateway["GW"],
"enabled":"true"
}
string=json.dumps(payload)

It's a Python dictionary and json.dumps turns it into valid json for the body. The header works because it's an HTML field and not json.

Get notified when there are additional replies to this discussion.