引数のない場合の使い方はこちらです。
Google Cloud Functions の使い方
index.js
/**
* HTTP Cloud Function.
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.gcf_post = (req, res) => {
var str_out = ""
str_out += "これはテストです。\n"
str_out += "Good Evening\n"
str_out += "今晩は\n"
str_out += "Mar/11/2018 PM 18:02\n"
const user = req.body['user']
const password = req.body['password']
str_out += "user = " + user + "\n"
str_out += "password = " + password + "\n"
res.send(str_out)
}
デプロイ
gcloud functions deploy function-nov25 \
--entry-point=gcf_post \
--trigger-http \
--runtime=nodejs22 --gen2 \
--region asia-northeast1
URL の確認
$ gcloud functions describe function-nov25 --region asia-northeast1 | grep url:
url: https://asia-northeast1-project-nov15-2024.cloudfunctions.net/function-nov25
Curl でテスト
curl_post.sh
URL="https://asia-northeast1-project-nov15-2024.cloudfunctions.net/function-nov25"
curl $URL/gcf_post \
-H "Authorization: bearer $(gcloud auth print-identity-token)" \
-d 'user=jiro&password=123456'
実行結果
$ ./curl_post.sh
これはテストです。
Good Evening
今晩は
Mar/11/2018 PM 18:02
user = jiro
password = 123456
Httpie でテスト
httpie_post.sh
URL="https://asia-northeast1-my-project-aug-29-2016.cloudfunctions.net"
http $URL"/gcf_post" \
"Authorization: bearer $(gcloud auth print-identity-token)" \
user=jiro password=123456
Get の引数の受け取り方はこちら
Google Cloud Functions で Get の引数を受け取る