次の Cloud Function を gcloud から使う方法です。
test-function-uchida-apr04
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.helloWorld = (req, res) => {
let message = req.query.message || req.body.message || 'Hello World! Apr/04/2022 PM 16:12';
res.status(200).send(message);
};
Region は asia-northeast1
Project は project-apr0401
とします。
$ gcloud functions list
NAME STATE TRIGGER REGION ENVIRONMENT
test-function-uchida-apr04 ACTIVE HTTP Trigger asia-northeast1 2nd gen
環境設定
$HOME/.config/gcloud/configurations/config_default
[core]
account = example@gmail.com
project = project-apr0401
Login
gcloud auth login
実行コマンド
gcloud functions call --region=asia-northeast1 test-function-uchida-apr04
実行結果
$ gcloud functions call --region=asia-northeast1 test-function-uchida-apr04
Hello World! Apr/04/2022 PM 16:12
Curl を使った例
curl -X GET "https://asia-northeast1-project-apr0401.cloudfunctions.net/test-function-uchida-apr04" \
-H "Authorization: bearer $(gcloud auth print-identity-token)"
echo ""
Httpie を使った例
http "https://asia-northeast1-project-apr0401.cloudfunctions.net/test-function-uchida-apr04" \
"Authorization: bearer $(gcloud auth print-identity-token)"