Handling returned data from GAS POST requests

208 views
Skip to first unread message

Jacob Edward

unread,
May 17, 2019, 5:23:33 PM5/17/19
to Google Apps Script Community
Haven't needed to return data from GAS to NodeJS so I've never had to write a jig for this redirect madness... considering to save returns to a utility .txt file that I can wget after I know the script completes but that would be considered inelegant lol...  I've verified the POST request is correct (unless there's some auto follow redirect parameter I don't know about), tried GET on the link but that didn't give me the correct output...  Any ideas?

return ContentService.createTextOutput("complete");

This is the response I'm getting from the post:

This is the response I'm getting from the get:


<!DOCTYPE html><html lang="en"><head><meta name="description" content="Web word processing, presentations and spreadsheets"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0"><link rel="shortcut icon" href="//ssl.gstatic.com/docs/common/drive_favicon1.ico"><title>Google Drive -- Page Not Found</title><link href="//fonts.googleapis.com/css?family=Product+Sans" rel="stylesheet" type="text/css"><style>/* Copyright 2019 Google Inc. All Rights Reserved. */
.goog-inline-block{position:relative;display:-moz-inline-box;display:inline-block}* html .goog-inline-block{display:inline}*:first-child+html .goog-inline-block{display:inline}#drive-logo{margin:18px 0;position:absolute;white-space:nowrap}.docs-drivelogo-img{background-image:url('//ssl.gstatic.com/images/branding/googlelogo/1x/googlelogo_color_116x41dp.png');background-size:116px 41px;display:inline-block;height:41px;vertical-align:bottom;width:116px}.docs-drivelogo-text{color:#000;display:inline-block;opacity:0.54;text-decoration:none;font-family:'Product Sans',Arial,Helvetica,sans-serif;font-size:32px;text-rendering:optimizeLegibility;position:relative;top:-6px;left:-7px;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}@media (-webkit-min-device-pixel-ratio:1.5),(min-resolution:144dpi){.docs-drivelogo-img{background-image:url('//ssl.gstatic.com/images/branding/googlelogo/2x/googlelogo_color_116x41dp.png')}}</style><style type="text/css">body {background-color: #fff; font-family: Arial,sans-serif; font-size: 13px; margin: 0; padding: 0;}a, a:link, a:visited {color: #112ABB;}</style><style type="text/css">.errorMessage {font-size: 12pt; font-weight: bold; line-height: 150%;}</style></head><body><div id="outerContainer"><div id="innerContainer"><div style="position: absolute; top: -80px;"><div id="drive-logo"><a href="/"><span class="docs-drivelogo-img" title="Google logo"></span><span class="docs-drivelogo-text">&nbsp;Drive</span></a></div></div><div align="center"><p class="errorMessage" style="padding-top: 50px">Sorry, unable to open the file at this time.</p><p> Please check the address and try again. </p><div style="background: #F0F6FF; border: 1px solid black; margin-top: 35px; padding: 10px 125px; width: 300px;"><p><strong>Get stuff done with Google Drive</strong></p><p>Apps in Google Drive make it easy to create, store and share online documents, spreadsheets, presentations and more.</p><p>Learn more at <a href="https://drive.google.com/start/apps">drive.google.com/start/apps</a>.</p></div></div></div></div></body><style>#outerContainer {margin: auto; max-width: 750px;}#innerContainer {margin-bottom: 20px; margin-left: 40px; margin-right: 40px; margin-top: 80px; position: relative;}</style></html>


Jacob Edward

unread,
May 17, 2019, 5:51:34 PM5/17/19
to google-apps-sc...@googlegroups.com
Ah, figured it out, basically like this:

var req = g.https.request(options, function (res) {
  if (res.headers.location) {
    console.log(res.headers.location);
    GET(res.headers.location,callback);
    return;
  }
  
  var body = "";
  res.on('data', function (d) {body += d;});
  res.on('end', function () {
    callback(body);
  });
});

req.on('error', function (e) {console.error(e);});
req.write(postData);
req.end();

If the headers have the location attribute you need to break out of the handlers and remake the request with GET

Reply all
Reply to author
Forward
0 new messages