1
votes

I am trying to make a GET request to an external API from a Google Apps Script using UrlFetchApp. When I make this request with Postman or curl, I get back the expected response. However, when I try it with UrlFetchApp, I get back an empty response, {}.

I have tried using Basic Auth and OAuth 2, as well as explicitly setting the oauthScopes property in the manifest as described here.

I have confirmed with the API team that they are indeed sending back a full response when I hit the endpoint, but all I receive is {}. My problem seems similar to this StackOverflow question which went unanswered.

var headers = {
   "X-Client-Key": "KEY",
   "Authorization": "Bearer TOKEN"
};

var options = {
  method: "get",
  headers: headers,
}

var response = UrlFetchApp.fetch(ENDPOINT, options);
console.log(JSON.stringify(response));  // returns {}
1
What makes you think you only receive {}? - TheMaster
I was logging the response (updated the post). As @oleg-valter pointed out, this doesn't work correctly with an HTTPResponse object. Thanks for the hint! - Lauren
If you use Logger.log(response) you will see all the response. - camden_kid

1 Answers

4
votes

Do not take what you see in logs at face value. fetch method of the UrlFetchApp service always returns an instance of HTTPResponse which is an object first and foremost. This is what the logs show you (I am assuming you are logging the response because this is the only context I am aware of where {} is displayed).

To extract useful information from the response, use the appropriate methods exposed on HTTPResponse instances, like getResponseCode or getContentText.