LoginSignup
0
0

More than 5 years have passed since last update.

API Gateway のリスポンスヘッダーのカスタマイズ (Python3)

Last updated at Posted at 2017-11-05

次の記事のプログラムは Node.js で書きましたが、それを Python3 に書き換えました。
API Gateway のリスポンスヘッダーのカスタマイズ (Node.js)

# -*- coding: utf-8 -*-
#
#                   Nov/5/2017
#
# --------------------------------------------------------------------
import sys
import json

# --------------------------------------------------------------------
def py01_handler(event, context):
    sys.stderr.write("*** py01_handler *** start ***\n")
#
    aa = 0
    bb = 0
    cc = 0
#
    sys.stderr.write("Received event.body: " + json.dumps(event) + "\n")
    if ('body' in event):
        json_str = event['body']
        unit_aa = json.loads(json_str)
        if ('aa' in unit_aa):
            sys.stderr.write("aa = " + str(unit_aa['aa']) + "\n")
            aa = int(unit_aa['aa'])
#
        if ('bb' in unit_aa):
            sys.stderr.write("bb = " + str(unit_aa['bb']) + "\n")
            bb = int(unit_aa['bb'])
#
        if ('cc' in unit_aa):
            sys.stderr.write("cc = " + str(unit_aa['cc']) + "\n")
            cc = int(unit_aa['cc'])
#
    version = "Nov/5/2017 PM 20:43"
#
    sum = aa + bb + cc
#
    sys.stderr.write("version = " + version + "\n")
    sys.stderr.write("sum = " + str(sum) + "\n")
    sys.stderr.write("*** py01_handler *** end ***\n")
#
    body = {}
    body['aa'] = aa
    body['bb'] = bb
    body['cc'] = cc
    body['sum'] = sum
    body['version'] = version
    body['language'] = 'Python'
#
    rvalue = {}
    rvalue['statusCode'] = 200
    headers = {}
    headers["X-Custom-Header"] = "Shimotsuke " + version
    headers["X-Powered-By"] = "Python3"
#        headers["Status"] = "200 OK"
    headers["Status"] = "401 Unauthorized"
    rvalue['headers'] = headers
    rvalue['body'] = json.dumps(body)
#
#
    return rvalue
#
# --------------------------------------------------------------------
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