2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GoogleAppsScriptで公開するURLはリダイレクトされる

Posted at

タイトルの通りです。
GoogleAppsScriptでウェブアプリケーションとして公開している方も多いかと思います。
ブラウザなどリダイレクトに対応しているクライアントであれば特に意識する必要もありません。

アレクサのカスタムスキルを作っていて、AWSLambda上のNode.jsでhttpsモジュールを使用してこの件にハマってしまいました。
それでレスポンスのステータスコードを見ると302で、GASの公開URLにブラウザからアクセスしてみると確かにリダイレクトされていました。

調べたところnodeでもリダイレクトに対応したモジュールはあるみたいですが、Lambda上で使用したい場合はzipにしてアップロードするようで面倒です。
とりあえず動けばいいやとLocationヘッダを見てもう一度リクエストするようにしたらGASから値を持って来れました。

// アレクサ用のhandlerから呼び出す関数
function call_api() {
  return new Promise((resolve)=> {
    const dist_url = "https://script.google.com/macros/s/***/exec";

    https.get(dist_url, function(res) {
      const status_code = res.statusCode;
      if (status_code == 302 && res.headers.location ) {

        https.get(res.headers.location, function(res) {
          let body = '';
          res.setEncoding('utf8');
          res.on('data', (chunk) => {
            body += chunk;
          });
          res.on('end', (res) => {
            res = JSON.parse(body);
            resolve(res);
          });
        });

      }
    });

  });
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?