7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google Cloud FunctionsのPython(Beta)でHttpトリガーのときJSONを返す

Last updated at Posted at 2018-08-13

概要

関数作成のサンプルを見る限り、Flaskが利用できるみたいだったので、試してみました。

手順

> cd 任意のディレクトリ
> touch main.py
> vi main.py
main.py
from flask import jsonify, make_response

def flask_json_response(request):
    return make_response(jsonify({'hoge': 'fuga'}))

で、デプロイします。

> gcloud beta functions deploy flask_json_response \
    --trigger-http \
    --runtime=python37

ポイントとしてはFlaskをpip installしてなくてもCloud Functions上では利用できる状態になっているってことでしょうか?
けど、実際にはローカル開発時にpip installしそう。どうでしょう?

で、呼び出します。

> curl --include https://us-central1-プロジェクトID.cloudfunctions.net/flask_json_response

HTTP/1.1 200 OK
Content-Type: application/json
Function-Execution-Id: a55fsn8a2dc7
X-Cloud-Trace-Context: 9b776cf314416e08c181efaab7524a1e
Date: Thu, 09 Aug 2018 06:33:42 GMT
Server: Google Frontend
Content-Length: 21
Alt-Svc: quic=":443"; ma=2592000; v="44,43,39,35"

{
  "hoge": "fuga"
}

はい。

Content-Type も自動で、application/json になってくれてますね。

わかれば簡単なのですが、なにせ公式ドキュメントがまだないので^^;;;

参考

Flask Application Object
http://flask.pocoo.org/docs/0.12/api/#flask.Flask.session_interface

もっとも簡単なFlaskの作り方
https://qiita.com/5zm/items/f283c201344ae347ea2d

Cloud FunctionsでPython利用記事まとめ
https://qiita.com/kai_kou/items/2f65db5305ba280ad81e

7
5
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
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?