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

SOLVED
JS-one
Conversationalist

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

Hi,

 

I've written a function using Google Apps Script to create an organization in Meraki but received above error saying there's a problem in the JSON.

I was able to GET data from Meraki but unable to PUT data using below format.

Any advice why this is happening?

 

 

function createOrg(orgName)
{
  var org = 
      {
        "name": orgName
      };
  var URL = "<a href="https://api.meraki.com/api/v0/organizations" target="_blank">https://api.meraki.com/api/v0/organizations</a>";
  var params = 
      {
        headers:
        {
          "X-Cisco-Meraki-API-Key": settings.apiKey, 
          "Content-Type": "application/json", 
          "Accept": "*/*"
        },
        method: "PUT",
        payload: org,
        muteHttpExceptions: true
      }
  var response = UrlFetchApp.fetch(URL, params);
  Logger.log(response);
}

 

 

1 ACCEPTED SOLUTION
BrechtSchamp
Kind of a big deal

A few things. The create org call is supposed to be a POST instead of PUT.

 

I noticed that you need to apply a function to the payload:

payload: JSON.stingify(org)

 

Replace the baseURL 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/zGeneral_Administration/Other_Topics/The_Cisco_Meraki_Dashboard_API... (look for the section about redirection)

 

So this works for me:

function createOrg(orgName)
{
  var org = 
      {
        "name": "Google Apps Org 2"
      };
  var URL = "https://<myshardid>.meraki.com/api/v0/organizations";
  var params = 
      {
        headers:
        {
          "X-Cisco-Meraki-API-Key": "<myAPIkey>", 
          "Content-Type": "application/json", 
          "Accept": "*/*"
        },
        method: "POST",
        payload: JSON.stringify(org),
        muteHttpExceptions: true
      }
  var response = UrlFetchApp.fetch(URL, params);
  Logger.log(response);
}

 

View solution in original post

2 REPLIES 2
BrechtSchamp
Kind of a big deal

A few things. The create org call is supposed to be a POST instead of PUT.

 

I noticed that you need to apply a function to the payload:

payload: JSON.stingify(org)

 

Replace the baseURL 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/zGeneral_Administration/Other_Topics/The_Cisco_Meraki_Dashboard_API... (look for the section about redirection)

 

So this works for me:

function createOrg(orgName)
{
  var org = 
      {
        "name": "Google Apps Org 2"
      };
  var URL = "https://<myshardid>.meraki.com/api/v0/organizations";
  var params = 
      {
        headers:
        {
          "X-Cisco-Meraki-API-Key": "<myAPIkey>", 
          "Content-Type": "application/json", 
          "Accept": "*/*"
        },
        method: "POST",
        payload: JSON.stringify(org),
        muteHttpExceptions: true
      }
  var response = UrlFetchApp.fetch(URL, params);
  Logger.log(response);
}

 

@BrechtSchampthank you so much for your advice, I was able to create the organization successfully.

Get notified when there are additional replies to this discussion.