LoginSignup
8
10

More than 1 year has passed since last update.

Google Cloud Functions で Post の引数を受け取る

Last updated at Posted at 2018-03-11

引数のない場合の使い方はこちらです。
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)
}

function_apr04.png

デプロイ

gcloud functions deploy gcf_post --source . --trigger-http \
--runtime=nodejs16 \
--region asia-northeast1

Curl でテスト

curl_post.sh
HOST="https://asia-northeast1-my-project-aug-29-2016.cloudfunctions.net"
curl $HOST/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
HOST="https://asia-northeast1-my-project-aug-29-2016.cloudfunctions.net"
http $HOST"/gcf_post" \
	"Authorization: bearer $(gcloud auth print-identity-token)" \
	user=jiro password=123456

Get の引数の受け取り方はこちら
Google Cloud Functions で Get の引数を受け取る

8
10
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
8
10