8
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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 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 の引数を受け取る

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?