0
0

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: 加算

Last updated at Posted at 2022-04-04

プログラム

index.js
/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
exports.sum_up = (req, res) => {
  var str_out = ""
  str_out += "これはテストです。\n"
  str_out += "Good Evening\n"
  str_out += "今晩は\n"
  str_out += "Apr/04/2022 PM 18:02\n"
  const aa = req.body['aa']
  const bb = req.body['bb']
  const sum = parseInt(aa) + parseInt(bb)
  str_out += "aa = " + aa + "\n"
  str_out += "bb = " + bb + "\n"
  str_out += "sum = " + sum + "\n"
  res.send(str_out)
};
$ gcloud functions list
NAME                  STATE   TRIGGER       REGION           ENVIRONMENT
test-function-sum_up  ACTIVE  HTTP Trigger  asia-northeast1  2nd gen

curl のテストスクリプト

curl_post.sh
URL="https://asia-northeast1-project-nov15-2024.cloudfunctions.net/test-function-sum_up"
#
curl $URL \
	-H "Authorization: bearer $(gcloud auth print-identity-token)" \
	-d 'aa=12&bb=45'
echo ""

実行結果

$ ./curl_post.sh 
これはテストです。
Good Evening
今晩は
Apr/04/2022 PM 18:02
aa = 12
bb = 45
sum = 57

Httpie のテストスクリプト

--verify=no をつけないとエラーになります。

httpie_post.sh
URL="https://asia-northeast1-project-nov15-2024.cloudfunctions.net/test-function-sum_up"
#
http POST $URL --verify=no \
	"Authorization: bearer $(gcloud auth print-identity-token)" \
	aa=19 bb=35

実行結果

$ ./httpie_post.sh 
HTTP/1.1 200 OK
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
Content-Length: 97
Date: Mon, 25 Nov 2024 05:06:37 GMT
Server: Google Frontend
X-Cloud-Trace-Context: f87268e97676d0c56c0536bce3605dd7;o=1
content-type: text/html; charset=utf-8

これはテストです。
Good Evening
今晩は
Apr/04/2022 PM 18:02
aa = 19
bb = 35
sum = 54

使ったバージョン

$ httpie --version
3.2.2
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?