【问题标题】:How should I set integration request for a lambda function in API gateway我应该如何在 API 网关中设置 lambda 函数的集成请求
【发布时间】:2019-03-08 19:56:11
【问题描述】:

我有以下由 API 网关调用的 lambda 函数

import sys
import logging
import pymysql
import json
rds_host="rds.amazonaws.com"
name="name"
password="pass"
db_name="DB"
port = 3306
def save_events(event):
result = []
conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, 
connect_timeout=30)
Bid=pymysql.escape_string("3")
with conn.cursor(pymysql.cursors.DictCursor) as cur:
 cur.execute("select exid,exercise_name,image from exercise where bid = 3")
 result = cur.fetchall()
cur.close()
print ("Data from RDS...")
print (result)
workout = json.dumps(result)
workouts=(workout.replace("\"", "'"))

def lambda_handler(event, context):
 save_events(event)
return workouts

现在在 api 网关中 get 方法的集成请求中,我应该在映射模板中添加什么以从用户获取 json 格式的数据,以及如何在查询中传递该用户值(例如:select exid、exercise_name、image from练习,其中出价 =“用户提供的价值”)。是 AWS 和后端开发的初学者。在此先感谢

【问题讨论】:

    标签: amazon-web-services aws-lambda aws-api-gateway amazon-rds


    【解决方案1】:

    您可以传递查询参数。为此,您必须在集成请求和映射模板中提及查询字符串参数,将 json/application 添加到 lambda 代码中的映射参数。 cur.execute("select exid,exercise_name,从出价 = 3 的练习中选择图像") 将此行替换为

    cur.execute("select exid,exercise_name,image from exercise where bid = event['params']['querystring']['parameter_name']")
    

    有关更多信息,请查看此 pass parameter to lambda from api gateway

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2021-04-22
      • 1970-01-01
      • 2022-07-05
      • 1970-01-01
      • 2018-03-06
      • 2017-02-17
      相关资源
      最近更新 更多