ImportJSON for Google Scripts error: "TypeError: transformFunc is not a function" - json

I am trying to get a JSON file to a google spreadcheet.
The the API call is made and the JSON file is retrieved with the "ImportJSONAdvanced" function from ImportJSON bradjasper version 1.5.
But when it reaches line 422, it gives an error saying "TypeError: transformFunc is not a function".
These are the arguments I am passing:
const URL_query = 'URL1='+ myquery
const API_Call = {
headers: { 'method_for_key': 'my_apy_key' },
json: true,
gzip: true,
}
var parsedJSON = ImportJSONAdvanced(URL_query, API_Call)

if you check the source code, you'll see that you are obviously missing some arguments to ImportJSONAdvanced. Specifically, the error message is complaining about missing the last argument, transformFunc but probably this is not the only error you will get.

Related

Apps Script - How to solve a truncated output from the console [duplicate]

I'm getting an error response from the API that I'm using, but Google scripts seems to truncate the message. How can I see the full message in Google scripts?
This is the message:
Request failed for https://api.myintervals.com/task/ returned code 400. Truncated server response: {"personid":"180761","status":"Bad Request","code":400,"error":{"code":18,"message":"Validation error has occurred (missing required field/paramet... (use muteHttpExceptions option to examine full response) (line 171, file "IntervalsPull")
Just as #DrSatan1 pointed in the comment, pass muteHttpExceptions option in the parameter to suppress the exception and get the error returned as HTTPResponse.
options = {muteHttpExceptions: true};
var response = UrlFetchApp.fetch("https://api.myintervals.com/task/", options);
Logger.log(response.getContentText());
Now view your logs to see the complete error response.
While setting response to logger or console works, even that will be truncated if the response is too long. You may have to set the response to Drive in such cases.
options.muteHttpExceptions = true;
const res = UrlFetchApp.fetch(url, options);
console.log(res.getResponseCode());
console.log(res);
DriveApp
.getFoldersByName('test'/*A folder in Drive*/)
.next()
.createFile(res.getBlob().setName('response'))

Postman Test - tv4.validateResult TypeError: Cannot read property '$ref' of undefined

I am using postman, i have added a test to validate the schema of the response.
The schema and response schema return as expected, but somewhere in the following code i get the error "tv4.validateResult TypeError: Cannot read property '$ref' of undefined".
pm.test(tag + ".SCHEMA response matches required schema", function() {
var result = tv4.validateResult(jsonRes, schema.response);
if (!result.valid) {console.log(result);}
pm.expect(result.valid).to.be.true;
});
I switched to this little magic script
pm.test(tag + ".SCHEMA response matches required schema", function() {
var valid = tv4.validate(jsonRes, schema, false, true);
pm.expect(valid).to.be.true;
});
and it worked. Not sure why, hoping for someone to add some clarification

getResponseCode() method doesn't return expected response code

I have 2 questions about Google App Script Services getResponseCode() method.
1) "Unexpected Error"
When I run the getResponseCode() method, I got "Unexpected Error...".
https://developers.google.com/apps-script/reference/url-fetch/http-response#getResponseCode()
var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
responseCode = response.getResponseCode();
Unexpected error: https://www.example.com/
※I can't tell the url for business reasons.
HTTP response status codes don't include "Unexpected Error".
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
Please tell me what response codes actually return, when this error occurs?
2) getResponseCode() method didn't work as expected
When I run the code below, I got "200".
I expected "301" in response to the "http://google.com/" request.
function myFunction() {
var response = UrlFetchApp.fetch("http://google.com/");
 Logger.log(response.getResponseCode());
}
I think getResponseCode() method doesn't return actual http status codes.
Please tell me why I got "200" instead of "301".
get 301 response in browser
get 200 response instead of 301
This happens because the request is following the redirect. Take a look at the available parameters in the UrlFetchApp.fetch() method. You'll see followRedirects, which defaults to true.
Make this small change and you'll get the expected 301.
function myFunction() {
var response = UrlFetchApp.fetch("http://google.com/", { followRedirects: false });
 Logger.log(response.getResponseCode());
}

Why am I getting an undefined value when calling these scripts in Google App Maker?

I do not understand why when I am calling a ServerScript method from a ClientScript method, I am getting a value of undefined.
ClientScript:
function clientScript() {
var message;
message = google.script.run.test();
console.log("Message: " + message);
}
ServerScript:
function serverScript() {
return "hello";
}
I expected the console to print: Message: hello. However, I am getting this printed to my console: Message: undefined. Why am I getting an undefined value in my ClientScript method when I am returning a defined value in my ServerScript method? Thank you!
Because server calls are asynchronous. In order to handle server response you need to pass callback. Here is a snippet from Apps Script docs:
function onSuccess(numUnread) {
console.log(numUnread);
}
google.script.run.withSuccessHandler(onSuccess)
.getUnreadEmails();
Just in case AMs docs interpretation of the same thing - https://developers.google.com/appmaker/scripting/client#call_a_server_script

Invalid Argument on basic API call using UrlFetchApp.fetch(url)

I'm new to Google Apps Scripts and I've been trying to make a simple Get call to a URL. I make this call from my browser: https://accounting.sageone.co.za/api/1.1.1/Supplier/Get?apikey={4CFEEFE1-CE04-425F-82C3-DCB179C817D5}&companyid=164740 and get the respons I'm looking for. I now try to make the call from Google Apps Scripts using the following code:
function myFunction() {
var url = 'https://accounting.sageone.co.za/api/1.1.1/Supplier/Get?apikey={4CFEEFE1-CE04-425F-82C3-DCB179C817D5}&companyid=164740
var response = UrlFetchApp.fetch(url);
Logger.log(response);
}'
I get a respons stating '
Message details
Invalid argument: https://accounting.sageone.co.za/api/1.1.1/Supplier/Get?apikey={4CFEEFE1-CE04-425F-82C3-DCB179C817D5}&companyid=164740 (line 4, file "Code")'
I've tried a whole bunch of permutations with no luck...
When using UrlFetchApp, you need to enter your URL parameters as part of a request parameters rather than in the URL itself. For a GET request these go directy as part of the parameters, for a POST request the parameters would be part of a payload object. Reference Documentation
Here is a modified function:
function myFunction() {
var url = 'https://accounting.sageone.co.za/api/1.1.1/Supplier/Get'
var params = {
"method": 'GET',
"apikey": "{4CFEEFE1-CE04-425F-82C3-DCB179C817D5}",
"companyid": "164740"
}
var response = UrlFetchApp.fetch(url, params);
Logger.log(response)
}
Note: This corrects your method, however, the server still requires further authentication. If you run into issues with that, ask another questions specific to that issue as well.