After an outage on September 1, the Instructure Community is now fully available, including guides, release notes, forums, and groups. If some styling still looks unusual, clear your cache and cookies.
Found this content helpful? Log in or sign up to leave a like!
Hi there,
I have started setting up a Google Script environment to handle certain things via the Canvas API for my team.
So far, I created only POST/PUT requests which are working just fine. Now I wanted to implement a GET request, however, the response returns HTML code instead of JSON data. The request works fine in Postman and a NodeJS version as well, so I'm sure I'm missing something in GScript. Did anyone encounter this issue? Below is my sample code.
function getRequestTest() {
var url = `myUrl.com/api/v1/courses/2555/users`;
var token = "myToken";
var options = {
method : "get",
contentType: "application/json",
payload : JSON.stringify(data),
headers : { Authorization: "Bearer " + token },
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, options).getContentText();
Logger.log(response);
}
The Logger literally prints out the HTML code of the course page.
Thanks for your help in advance!
Tamas
Solved! Go to Solution.
Hi, Tamas. I think there are two things going on here that might be affecting the output.
1) A GET request shouldn't need a payload parameter. The options object should only have method, header, and muteHttpExceptions.
2) You should JSON.parse() directly the item returned by fetch function.
See if this gets you going. Good luck!
Hi, Tamas. I think there are two things going on here that might be affecting the output.
1) A GET request shouldn't need a payload parameter. The options object should only have method, header, and muteHttpExceptions.
2) You should JSON.parse() directly the item returned by fetch function.
See if this gets you going. Good luck!
Thank you, @melodyc_lam! Exactly that was the issue, now it works like a charm!
Have a great day!
Tamas
Based on the Google Apps Script documentation, it looks like you need to parse your response variable to get the JSON format: https://developers.google.com/apps-script/guides/services/external#work_with_json
// Make request to API and get response before this point.
var json = response.getContentText();
var data = JSON.parse(json);
Logger.log(data.title);
To interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign inTo interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign in