HTML
post
python3
APIGateway

API Gateway で Post で引数を与えて html を返す

API Gateway で Post で引数を与えて 日本語を含む html を返す方法です。

Lambda のプログラムです。

display_address.py
# -*- coding: utf-8 -*-
#
#   display_address.py
#
#                   Nov/18/2017
# --------------------------------------------------------------------
import  sys
import  json
import  boto3
#
# --------------------------------------------------------------------
def display_address_handler(event, context):
    sys.stderr.write("*** display_address_handler *** start *** PM 20:08\n")
    prefecture = "No prefecture"
    city = "No city"
    town = "No town"
#
    sys.stderr.write("Received event: " + json.dumps(event, indent=2) + "\n")

    if ('body' in event):
        json_str = event['body']
        unit_aa = json.loads(json_str)
        if ('prefecture' in unit_aa):
            prefecture = unit_aa['prefecture']
#
        if ('city' in unit_aa):
            city = unit_aa['city']
#
        if ('town' in unit_aa):
            town = unit_aa['town']
#
    sys.stderr.write("prefecture = " + prefecture + "\n")
    sys.stderr.write("city = " + city + "\n")
    sys.stderr.write("town = " + town + "\n")

    version = "Nov/18/2017 AM 08:45"
    sys.stderr.write("version: " + version + "\n")

#
    sys.stderr.write("*** display_address_handler *** pppp ***\n")
#
    rvalue = {}
    rvalue['statusCode'] = 200
    headers = {}
    headers["Content-Type"] = "text/html; charset=UTF-8"
    headers["X-Custom-Header"] = "Shimotsuke " + version
    headers["X-Powered-By"] = "Python3"
    headers["Status"] = "200 OK"
#   headers["Status"] = "401 Unauthorized"
#
    rvalue['headers'] = headers
#
    html_str = "<html>"
    html_str += "<head>"
    html_str += "<title>Address</title>"
    html_str += "</head>"
    html_str += "<body>"
    html_str += "<h2>Hello</h2>"
    html_str += prefecture + "<br />"
    html_str += city + "<br />"
    html_str += town + "<br />"
    html_str += "</body>"
    html_str += "</html>"
    rvalue['body'] = html_str
#
    sys.stderr.write("*** display_address_handler *** end ***\n")
#
    return rvalue
#
# --------------------------------------------------------------------

AWS のコンソールで、API Gateway の設定をします。
Lambda プロキシ統合の使用にチェックを入れて下さい。

display_nov1801.png

確認のプログラムです。

#
REST_API_ID=a9ov7k9qhk
#
URL="https://"$REST_API_ID".execute-api.ap-northeast-1.amazonaws.com/test"
#
curl  -H "Content-Type: application/json" -d@in01.json $URL
#
in01.json
{
    "prefecture": "栃木",
    "city": "下野",
    "town": "薬師寺"
}
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   restapi_test.py
#
#                   Nov/18/2017
# ----------------------------------------------------------------
import sys
import json
import requests
# ----------------------------------------------------------------
#
rest_api_id="a9ov7k9qhk"
#
url="https://" + rest_api_id + ".execute-api.ap-northeast-1.amazonaws.com/test"
#
# print(url)
#
payload = {
    "prefecture": "栃木",
    "city": "下野",
    "town": "薬師寺"
    }
#
try:
    rr = requests.post(url, data=json.dumps(payload))
    print(rr.headers)
    print()
    print(rr.text)
except Exception as ee:
    sys.stderr.write("*** error *** request.get ***\n")
    sys.stderr.write(str(ee) + "\n")
#
# ----------------------------------------------------------------
#! /usr/bin/node
// ---------------------------------------------------------------
//
//  restapi_test.js
//
//                  Nov/18/2017
//  
// ---------------------------------------------------------------
const rest_api_id="a9ov7k9qhk"

console.log ("*** start ***")

var Client = require('node-rest-client').Client

var client = new Client()

const params = {"prefecture": "栃木",
    "city": "下野",
    "town": "薬師寺"
    }

const args = {
    data: params,
    headers: { "Content-Type": "application/json" }
}


const url="https://" + rest_api_id + ".execute-api.ap-northeast-1.amazonaws.com/test"


client.post (url, args, function (data,response)
    {
    console.log(data.toString('utf-8'))

    console.log ("*** end ***")
    })

//
// ---------------------------------------------------------------