Error Code 400 - PUT Script

Solved
Vmadathil
Here to help

Error Code 400 - PUT Script

HI All,

When I applied the script for replacing "RADIUS Servers", I encountered the following error code. However, It is working as expected on the "Sandbox".

Updated RADIUS server radius1 for SSID ONE-1900
Updated RADIUS server radius2 for SSID ONE-1900
Updated RADIUS server radius3 for SSID ONE-1900
Failed to update SSID configuration for ONE-1900. Status code: 400

 

Script:-

import requests

# Define your API key, network ID, and base URL
API_KEY = 'YOUR_API_KEY'
NETWORK_ID = 'YOUR_NETWORK_ID'
BASE_URL = 'https://api.meraki.com/api/v1'

# Define the old and new radius server values
OLD_RADIUS_SERVERS = {
'radius1': 'OLD_RADIUS_SERVER_IP_1',
'radius2': 'OLD_RADIUS_SERVER_IP_2',
'radius3': 'OLD_RADIUS_SERVER_IP_3'
}

NEW_RADIUS_SERVERS = {
'radius1': 'NEW_RADIUS_SERVER_IP_1',
'radius2': 'NEW_RADIUS_SERVER_IP_2',
'radius3': 'NEW_RADIUS_SERVER_IP_3'
}

# Define headers with API key
headers = {
'X-Cisco-Meraki-API-Key': API_KEY,
'Content-Type': 'application/json'
}


# Function to update RADIUS server configuration
def update_radius_config(ssid_config):
# Check if RADIUS servers are configured
if 'radiusServers' in ssid_config:
radius_servers = ssid_config['radiusServers']
updated = False
for server in radius_servers:
for old_name, old_host in OLD_RADIUS_SERVERS.items():
if server['host'] == old_host:
new_host = NEW_RADIUS_SERVERS[old_name]
server['host'] = new_host
updated = True
print(
f"Updated RADIUS server {old_name} for SSID {ssid_config['name']}"
)

if updated:
# Make a PUT request to update the SSID configuration
ssid_config_url = f"{BASE_URL}/networks/{NETWORK_ID}/wireless/ssids/{ssid_config['number']}"
put_response = requests.put(ssid_config_url,
json=ssid_config,
headers=headers)
if put_response.status_code == 200:
print(
f"SSID configuration updated successfully for {ssid_config['name']}"
)
else:
print(
f"Failed to update SSID configuration for {ssid_config['name']}. Status code: {put_response.status_code}"
)


# Get SSIDs of the network
url = f"{BASE_URL}/networks/{NETWORK_ID}/wireless/ssids"
response = requests.get(url, headers=headers)
if response.status_code == 200:
ssids = response.json()
for ssid in ssids:
update_radius_config(ssid)
else:
print("Failed to retrieve SSIDs.")

1 Accepted Solution

@Vmadathil That did work for me. Just add a line before the put operation.

 

ssid_config['encryptionMode'] = 'wep'
put_response = requests.put(ssid_config_url,json=ssid_config,headers=headers)

 

View solution in original post

13 Replies 13
alemabrahao
Kind of a big deal
Kind of a big deal

I think you need to include a variable in your script to include the following information.

authMode ,encryptionMode and wpaEncryptionMode

 

https://developer.cisco.com/meraki/api-v1/update-network-wireless-ssid/

I am not a Cisco Meraki employee. My suggestions are based on documentation of Meraki best practices and day-to-day experience.

Please, if this post was useful, leave your kudos and mark it as solved.

I tested the script using the following parameters. But no luck. Got the same error.

{'host': 'radius_server_1_ip', 'port': radius_server_1_port, 'secret': 'radius_server_1_secret'},
{'host': 'radius_server_2_ip', 'port': radius_server_2_port, 'secret': 'radius_server_2_secret'},
{'host': 'radius_server_3_ip', 'port': radius_server_3_port, 'secret': 'radius_server_3_secret'}
]

PhilipDAth
Kind of a big deal
Kind of a big deal

My guess is that ssid_config contains a parameter that you got with f"{BASE_URL}/networks/{NETWORK_ID}/wireless/ssids" that can not be "put".

Have you considered using the Meraki Python SDK?  It makes stuff so much easier ...

It is working on Sandbox environment without any issues. I haven't used Python SDK.

On the basis of your message, I believe the issue is with the following part of the script. 

if updated:
# Make a PUT request to update the SSID configuration
ssid_config_url = f"{BASE_URL}/networks/{NETWORK_ID}/wireless/ssids/{ssid_config['number']}"
put_response = requests.put(ssid_config_url,
json=ssid_config,
headers=headers)
if put_response.status_code == 200:
print(
f"SSID configuration updated successfully for {ssid_config['name']}"
)
else:
print(
f"Failed to update SSID configuration for {ssid_config['name']}. Status code: {put_response.status_code}"
)

I changed many things but no luck. Could you please correct the script..

ShawnHu
Meraki Employee
Meraki Employee

Agreed with Philip that there might be something unexpected in the ssid_config from the GET call.
In your case, as you are updating the RadiusServers only, a more efficient way is to just include the updated RadiusSevers parameter in the json body instead of including the entire ssid_config. 

For example, if I want to update the SSID name, I can only have one parameter in the json body in the API PUT operation. Those other parameters not in the body will remain unchanged. Hopefully, this is helpful.{{baseUrl}}/networks/:networkId/wireless/ssids/:number

{
"name": "UPDATED-SSID-NAME"
}

I modified the line as follows. but no luck..

ssid_config_url = BASE_URL + "/networks/" + NETWORK_ID + "/wireless/ssids/" + str(ssid_config['number'])

Do you have any other recommendation.

Vmadathil
Here to help

 Let me briefly explain the requirement here.

We recently migrated our "RADIUS Servers" and IP got changed. So, we need to replace old RADIUS with new one. We have networks in all regions. According to the region (US, EMEA, APAC), the order of the RADIUS servers will be changed. We are using the same port and Radius key. Hence, the script should be capable of replacing given old radius servers with the new one in each ssids under a particular network. We are using same RADIUS servers in multiple SSIDs.

ShawnHu
Meraki Employee
Meraki Employee

Interesting. I was testing with Postman and everything was fine. But when using your code, I actually can reproduce the 400 error. You can edit the following line to get the error details.

print(
f"Failed to update SSID configuration for {ssid_config['name']}. Status code: {put_response.status_code}. Errors: {put_response.text}"
)

The error I got was "{"errors":["'encryptionMode' must be one of: 'wep' or 'wpa'"]}"

There were some discussions about this.
https://community.meraki.com/t5/Wireless/API-problems-with-update-SSID/m-p/199648
https://community.meraki.com/t5/Wireless/quot-Update-the-attributes-of-an-SSID-quot-AP-endpoint-brok...

Looks like the solution is to modify the 'encryptionMode' to either 'wep' or 'wpa'. Not sure why your script works in the sandbox environment. If there are further questions, I would suggest file a support case.

I tested this today, I am also getting the same error on the production network.

Updated RADIUS server radius1 for SSID ONE-1900
Updated RADIUS server radius2 for SSID ONE-1900
Updated RADIUS server radius3 for SSID ONE-1900
Failed to update SSID configuration for ONE-1900. Status code: 400. Errors: {"errors":["'encryptionMode' must be one of: 'wep' or 'wpa'"]}
>>>

Vmadathil
Here to help

Anybody could add the encryption mode and make it work. Please let me know if yes.

@Vmadathil That did work for me. Just add a line before the put operation.

 

ssid_config['encryptionMode'] = 'wep'
put_response = requests.put(ssid_config_url,json=ssid_config,headers=headers)

 

Vmadathil
Here to help

Thank you so much ShawnHuPhilipDAth  & alemabrahao.

Get notified when there are additional replies to this discussion.