400 Bad request: While creating a new network

Solved
Vishnu
Conversationalist

400 Bad request: While creating a new network

Hi,

The following is the python code I am using to create a new network. Tried so many times with little changes each time to figure out the problem but no luck.Here are the error codes and response I am getting:

 

error code : 400 bad request

response.text : '{"errors":["Invalid type"]}'

response.raise_for_status : 400 Client Error: Bad Request for url : https://n57.meraki.com/api/v0/organizations/XXX/networks

 

Help me in understanding the problem ! what exactly is wrong with the code/data format.

 

Thanks

import requests
import json



def isjson(data):
    try:
        json_object = json.loads(data)
    except ValueError:
        return False
    return True

url = 'https://api.meraki.com/api/v0/organizations/XXXXX/networks'

#payload = "  {\n    \"name\": \"office\",\n    \"timeZone\": \"CET\",\n    \"tags\": \"test\",\n    \"type\": \"wireless\"\n  }"

headers = {
    'X-Cisco-Meraki-API-Key': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'Content-Type': 'application/json'
    }
payload={
    'name':'office',
    'timeZone':'CET',
    'tags':'test',
    'type':'wireless'
    }
parsed=json.dumps(payload)

if isjson(parsed):
      response = requests.post(url,headers=headers,data=parsed)
      print(response.text)

 

 

1 Accepted Solution
Vishnu
Conversationalist

Thanks to All of you ! 

 

Its executed, and network created.

There was a mistake in the argument type:'combined', Changed it to appliance, its working now.  Apologies that I have given wireless in the type, which invited confusion in troubleshooting. 

 

Thanks for your support ! 

 

 

View solution in original post

11 Replies 11
NolanHerring
Kind of a big deal

I'm just starting to learn Python, so I apologize if I'm not much help here.

 

That being said:

 

Error 400 = Bad Request- You did something wrong, e.g. a malformed request or missing parameter.

 

Just in case, I would change your POST command from n57 to just API, in case your shard in not accurate.

The error message you show is showing n57 but the python script is showing API.  Any link between the two somewhere else we are not seeing?

 

Also you have # in front of PAYLOAD.  Assuming that is supposed to be there?

 

Here are some things I found that might assist you:

 

https://create.meraki.io/build/meraki-python-library-installation-guide/

https://github.com/meraki/automation-scripts

https://community.meraki.com/t5/Solutions-APIs/400-Bad-request-There-was-a-problem-in-the-JSON-you-s...

 

@MacuserJim  <---- Jim here has a screenshot on another thread which appears to show how to create a new network.

 

https://community.meraki.com/t5/Solutions-APIs/Creating-a-network-container-which-should-be-bound-to...

 

Good luck and let me know how it goes, would love to learn from this.  ^_^

Nolan Herring | nolanwifi.com
TwitterLinkedIn
Vishnu
Conversationalist

Thanks Nolan for your time and the links you posted.

 

Tried importing the Meraki package as @MacuserJim  did, and still getting the same error (bad request- invalid type).

 

@NolanHerring  I tried giving payload (string / json) directly to requests.post() as a data and ended up with the same error, Then I commented it to supply parsed json dictionary (jason=json.dumps(payload)).

 

@NolanHerring About url:  It is redirected to n57 from api.meraki,com. I think that is usual.

 

 

Error remains even after using meraki package. Any help would be great. 

jdsilva
Kind of a big deal

Right off the hop I can see that you're not specifying a valid timezone. For CET you'd want to be specifying something like "Europe/Berlin" or Europe/Paris". 

Vishnu
Conversationalist

Changed time zone to specific one like you said, still the same error. Installed postman an hour ago and sent the POST request it is also giving the same error. 

 

 

>>> response.text
'{"errors":["Invalid type"]}'
>>> response
<Response [400]>
>>>

PhilipDAth
Kind of a big deal
Kind of a big deal

Take a quick look at the documentation:

https://dashboard.meraki.com/api_docs#create-a-network

 

You'll first need to send a query to get your Organisation ID.  Check out the curl example as well.

 

jdsilva
Kind of a big deal


@Vishnu wrote:


'{"errors":["Invalid type"]}'


I'm guessing this is the problem, but wireless should be a valid type. I'm kinda confused here 😞

jdsilva
Kind of a big deal

Well... @Vishnu your code works fine for me. Now I'm even more confused why it doesn't work for you.

 

It even took the CET for the timezone...

 

Does the API key you're using have the correct permissions to create a network in the Org you're working with?

PhilipDAth
Kind of a big deal
Kind of a big deal

Just for the hell of it, try deleting:

type':'wireless'

And then manually type it back in.

 

I wonder if you have a strange non-printable character in that string.

PhilipDAth
Kind of a big deal
Kind of a big deal

Is there any chance this network already exists, potentially as another type already?

Vishnu
Conversationalist

Thanks to All of you ! 

 

Its executed, and network created.

There was a mistake in the argument type:'combined', Changed it to appliance, its working now.  Apologies that I have given wireless in the type, which invited confusion in troubleshooting. 

 

Thanks for your support ! 

 

 

jdsilva
Kind of a big deal

Meraki wrote:

 


The type of the new network. Valid types are 'wireless' (for MR), 'switch' (for MS), 'appliance' (for MX, Z-Series), 'phone' (for MC), or a space-separated list of those for a combined network.

 

 


https://api.meraki.com/api_docs#create-a-network

 

 

Get notified when there are additional replies to this discussion.