API GET request returns an HTML print instead of JSON data @GoogleAppScript

Jump to solution
tmsnvk
Community Explorer

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

Labels (4)
0 Likes
1 Solution
melodyc_lam
Community Coach
Community Coach

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!

View solution in original post