LoginSignup
1
5

More than 5 years have passed since last update.

GASをAPIとして公開したときのpostとgetのデータの受け取り方の違い

Last updated at Posted at 2018-08-06

コード

function doGet(e) {
  console.log(e.parameter)
}


function doPost(e){
  console.log(e.postData.contents)
}

叩く

GET

curl  -X GET 'https://script.google.com/macros/s/hoge/exec?key1=value1&key2=value2'

POST

curl  -X POST 'https://script.google.com/macros/s/hoge/exec'  \
-H "Accept: application/json" -H "Content-type: application/json"  \
-d '{"key1":"value1","key2":"value2"}'

ログ

両方とも

{"key1":"value1","key2":"value2"}

を得る。

ただ、GETの方はjsonで受け取るのに対し
POSTの方は文字列なので JSON.parse() シテヤル必要あり

因みに

doPost(e)はparameterも受け取れるので

function doPost(e){
  console.log(e.postData.contents)
  console.log(e.parameter)
}
curl  -X POST 'https://script.google.com/macros/s/hoge/exec?key3=value3'  \
-H "Accept: application/json" -H "Content-type: application/json"  \
-d '{"key1":"value1","key2":"value2"}'

とすれば両方受け取れる。
webhookとして使うときに重宝するのでぜひ。

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