LoginSignup
0
1

More than 5 years have passed since last update.

google apps script で ただ post されてきた テキストを返す

Last updated at Posted at 2018-11-22

google apps script で、ただ文字列を返してみよう。

参考
https://qiita.com/tanabee/items/c79c5c28ba0537112922

GASへ

https://script.google.com にアクセスし、 + 新規スクリプト を選択。
GAS エディタが立ち上がる。

コード作成

とりあえず、まずはPOST送信されてきたデータを返してみる。


function doPost(e){  //このeの中にPOSTされてきた情報が入っている(オブジェクト形式)

  var text = e.parameter.text;
  return ContentService.createTextOutput(text);

}



上記を保存。
変更する時は、
公開 > ウェブアプリケーションとして導入の
プロジェクトバージョンを変更しないとコードの変更が有効にならないので注意。

HTML


<form action="https://script.google.com/macros/s/[YOURIID]/exec" method="post" target="_blank">   <!-- methodはpostで -->

    <label>このテキストが帰ってくるぞ
        <input type="text" name="text" value="tarou">
    </label><br/>

    <input type="submit" value="送信">

</form>


これでPOST送信し、POST送信したデータを取得することができます。

直接ファイルPOST送信して結果をもらう

普通にPOSTしただけじゃリダイレクトされてエラーになる。
ということで、一旦POSTし、遷移先を取得。
その遷移先のURLからデータを取得。

CAKEPHP3 を利用


    //use Cake\Http\Client;
    public function test()
    {

        $http = new Client();
        $response = $http->post(
            'https://script.google.com/macros/s/[YOURID]/exec',
            [
                'text' => 'おっぴゃー'
            ]
        );


        $head = $response->headers();

        $res = $http->get(
            $head['Location'][0]
        );

        pd($res->body);//おっぴゃー
        die;


    }

0
1
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
0
1