1

I've got a Google Apps Script:

function MakeHTTPCall() {

  let resp = UrlFetchApp.fetch('https://something.cloudfunctions.net/returnStatusText');
  console.log('Response code: ' + JSON.stringify(resp.getResponseCode()));
  console.log('Returned text: ' + JSON.stringify(resp.getContentText()));
  return;
}

And a Google Cloud Function (node.js 8)

exports.returnStatusText = async(req, res) => {

  // Return codes
  const ERR_OK = 200;
  const ERR_STATUS_NO_CONTENT = 204;


  // Get the email address sent
  if ('undefined' === typeof req.body.emailAddress) {
    // email address is undefined
    console.log('No email address');
    return res.status(ERR_STATUS_NO_CONTENT).send('DOH!');

  }
  return res.status(ERR_OK).send('OK');

}

My main reference is https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app

What I expect is that the Google Cloud Function will log 'No email address' and the Google App Script will log 204 and 'DOH!'. What I get however is that the Google Cloud Function logs 'No email address' as expected, but the Google Apps Script logs 204 and the empty string. If I change ERR_STATUS_NO_CONTENT to 200, then the Google Apps Script log has 200 and 'DOH!' as expected. Is this a bug or am I missing something? I'm betting I'm missing something.

carlesgg97
  • 4,184
  • 1
  • 8
  • 24

2 Answers2

1

From the MDN web docs:

204 No Content

The HTTP 204 No Content success status response code indicates that the request has succeeded, but that the client doesn't need to go away from its current page. A 204 response is cacheable by default. An ETag header is included in such a response.

Essentially - 204 responses are meant to be returned by requests which have succeeded, but are not meant to return any data.

If you would like to return data with your successful HTTP response, please consider using any of the following status codes:

Community
  • 1
  • 1
carlesgg97
  • 4,184
  • 1
  • 8
  • 24
0

See this previous question: How to catch UrlFetchApp.fetch exception

You can use the undocumented advanced option "muteHttpExceptions" to disable exceptions when a non-200 status code is returned, and then inspect the status code of the response. More information and an example is available on this issue.

Adam Stevenson
  • 657
  • 3
  • 11
  • Thanks, but a) It's a documented feature now: https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch%28String,Object%29 b) The comment by carlesgg67 (https://stackoverflow.com/users/12152337/carlesgg97) is the one I needed – Bruce Kozuma Dec 04 '19 at 23:21