0

When I am calling API in Google appscript using method UrlFetchapp getting error as org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject

Google appscript using method UrlFetchapp getting error as org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject

 var url='https://us-sandbox-api.abcd.com/api/rest/v2/venue/get/venues';

   var EventData=[{
                        "StartDate": "07/22/2019 09:00",
                        "EndDate": "07/29/2019 17:00",
                         "VenueId": [12312312]

                 }]; 

var options = {
               "method" : "POST",
               "headers" : {   
                             "Content-Type": "application/json",
                             "Authorization" : "Bearer "+ Access_Token
                             },
                "payload":JSON.stringify(EventData)  
               };
           var resp=UrlFetchApp.fetch(url,options);
           var RespObj= JSON.parse(resp.getContentText()); 
sticky bit
  • 36,626
  • 12
  • 31
  • 42

2 Answers2

0

Interesting... I've never encountered this error before while using UrlFetchApp. I believe the content-type should be reference outside of the headers, try the code below.

See the UrlFetchApp supported format.

var EventData = {
                  "StartDate": "07/22/2019 09:00",
                  "EndDate": "07/29/2019 17:00",
                  "VenueId": [12312312]
                };

var options = {
                 "method" : "post",
                 "headers": {"Authorization" : "Bearer " + Access_Token },
                 "contentType": "application/json",
                 "payload": JSON.stringify(EventData)
              };
              
var resp = UrlFetchApp.fetch(url, options);
var RespObj = JSON.parse(resp.getContentText());
Kenson G
  • 1
  • 1
  • Getting error as unsupported media format(error code 415) after modifying code. The API is working in post man and getting result. – Mukundha CHar Jul 19 '19 at 17:56
  • As I am unable to test the output using your url API, could you please share any log containing the error? – Kenson G Jul 21 '19 at 01:45
  • Here is the error org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject. The response has jsonarray(json object inside another json object). I have other two API which has similar response and for those API also I am facing the same issue. I think URLFetch method will not support Json array. Please let me know if you have any suggestion on this. – Mukundha CHar Jul 22 '19 at 12:47
0

The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format.

I cannot open your URL without your credentials, but if I test your code for a different URL - it does work, so the error must lie in the interaction between your URL and your authorization request (and maybe also content type).

ziganotschka
  • 25,866
  • 2
  • 16
  • 33