当前位置: 首页 > 知识库问答 >
问题:

HTTP请求正文未通过AWS API网关到达AWS lambda函数

冷翼
2023-03-14
package com.spacecorpshandbook.ostium.lambda.handler

import java.util

import com.google.gson.Gson
import temp.{ApiGatewayProxyResponse, Appointment, CancelResponse}

/**
  * Amazon Lambda handler adapter for the Cancellation application
  */
class CancellationHandler {

  def cancelAppointment(appointment: Appointment): ApiGatewayProxyResponse = {

    System.out.println("++++ appointmentId is: " + appointment.getAppointmentId)

    val apiGatewayProxyResponse = new ApiGatewayProxyResponse
    val cancelResponse = new CancelResponse

    cancelResponse.setMessage("Cancelled appointment with id " + appointment.getAppointmentId)

    val gson: Gson = new Gson

    apiGatewayProxyResponse.setBody(gson.toJson(cancelResponse))

    apiGatewayProxyResponse.setStatusCode("200")

    val headerValues = new util.HashMap[String, String]

    headerValues put("Content-Type", "application/json")

    apiGatewayProxyResponse.setHeaders(headerValues)

    System.out.println("+++++ message before returning: " + apiGatewayProxyResponse.getBody)

    apiGatewayProxyResponse
  }

}
Execution log for request test-request
Fri Dec 09 11:14:40 UTC 2016 : Starting execution for request: test-invoke-request
Fri Dec 09 11:14:40 UTC 2016 : HTTP Method: PUT, Resource Path: /cancel-appointment
Fri Dec 09 11:14:40 UTC 2016 : Method request path: {}
Fri Dec 09 11:14:40 UTC 2016 : Method request query string: {}
Fri Dec 09 11:14:40 UTC 2016 : Method request headers: {Content-Type= application/json}
Fri Dec 09 11:14:40 UTC 2016 : Method request body before transformations: {
    "applicationId": "asdfsfa"
}
Fri Dec 09 11:14:40 UTC 2016 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=****************************************************************************************************************************************************************************************************************************************************************************************************************************************5c044d, X-Amz-Date=20161209T111440Z, x-amzn-apigateway-api-id=l5tcmj0vlk,  Accept=application/json, User-Agent=AmazonAPIGateway_l5tcmj0vlk, Host=lambda.us-east-1.amazonaws.com, X-Amz-Content-Sha256=857a062940a7fbb8134bad1c007e9975a10bd8323c39f6040e797a98e87ea1f6, X-Amzn-Trace-Id=Root=1-584a9220-9cd537954952cca7daee32bf, Content-Type=application/json}
Fri Dec 09 11:14:40 UTC 2016 : Endpoint request body after transformations: {"resource":"/cancel-appointment","path":"/cancel-appointment","httpMethod":"PUT","headers":{"Content-Type":" application/json"},"queryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"accountId":"456204981758","resourceId":"xznq3u","stage":"test-invoke-stage","requestId":"test-invoke-request","identity":{"cognitoIdentityPoolId":null,"accountId":"456204981758","cognitoIdentityId":null,"caller":"456204981758","apiKey":"test-invoke-api-key","sourceIp":"test-invoke-source-ip","accessKey":"ASIAJ5D7KU524H7CTTTQ","cognitoAuthenticationType":null,"cognitoAuthenticationProvider":null,"userArn":"arn:aws:iam::456204981758:root","userAgent":"Apache-HttpClient/4.5.x (Java/1.8.0_102)","user":"456204981758"},"resourcePath":"/cancel-appointment","httpMethod":"PUT","apiId":"l5tcmj0vlk"},"body":"{\n    \"applicationId\": \"asdfsfa\"\n}","isBase64Encoded":false}
Fri Dec 09 11:14:40 UTC 2016 : Endpoint response body before transformations: {"statusCode":"200","headers":{"Content-Type":"application/json"},"body":"{\"message\":\"Cancelled appointment with id null\"}"}
Fri Dec 09 11:14:40 UTC 2016 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, x-amzn-RequestId=adcadf25-be00-11e6-8855-75e96d772946, Connection=keep-alive, Content-Length=128, Date=Fri, 09 Dec 2016 11:14:39 GMT, Content-Type=application/json}
Fri Dec 09 11:14:40 UTC 2016 : Method response body after transformations: {"message":"Cancelled appointment with id null"}
Fri Dec 09 11:14:40 UTC 2016 : Method response headers: {Content-Type=application/json, X-Amzn-Trace-Id=Root=1-584a9220-9cd537954952cca7daee32bf}
Fri Dec 09 11:14:40 UTC 2016 : Successfully completed execution
Fri Dec 09 11:14:40 UTC 2016 : Method completed with status: 200

如果我添加了一个特定的方法,比如POST而不将其设置为Lambda代理集成,那么我确实会看到provide request body data将其添加到Lambda函数中,并正确地反序列化到POJO中,然后返回

Execution log for request test-request
Fri Dec 09 11:22:02 UTC 2016 : Starting execution for request: test-invoke-request
Fri Dec 09 11:22:02 UTC 2016 : HTTP Method: POST, Resource Path: /cancel-appointment
Fri Dec 09 11:22:02 UTC 2016 : Method request path: {}
Fri Dec 09 11:22:02 UTC 2016 : Method request query string: {}
Fri Dec 09 11:22:02 UTC 2016 : Method request headers: {}
Fri Dec 09 11:22:02 UTC 2016 : Method request body before transformations: {
    "appointmentId" : "sfssdf"
}
Fri Dec 09 11:22:02 UTC 2016 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=****************************************************************************************************************************************************************************************************************************************************************************************************************************************a8dc41, X-Amz-Date=20161209T112202Z, x-amzn-apigateway-api-id=l5tcmj0vlk, Accept=application/json, User-Agent=AmazonAPIGateway_l5tcmj0vlk, Host=lambda.us-east-1.amazonaws.com, X-Amz-Content-Sha256=875dad4d4e05f8c12a7ca8aeaf69046d4153fc7f910e1eff1959cb011e8313a0, X-Amzn-Trace-Id=Root=1-584a93da-f841704d9feb371b31e41cb9, Content-Type=application/json}
Fri Dec 09 11:22:02 UTC 2016 : Endpoint request body after transformations: {
    "appointmentId" : "sfssdf"
}
Fri Dec 09 11:22:02 UTC 2016 : Endpoint response body before transformations: {"statusCode":"200","headers":{"Content-Type":"application/json"},"body":"{\"message\":\"Cancelled appointment with id sfssdf\"}"}
Fri Dec 09 11:22:02 UTC 2016 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, x-amzn-RequestId=b4f5efce-be01-11e6-91c3-5b1e06f831e2, Connection=keep-alive, Content-Length=130, Date=Fri, 09 Dec 2016 11:22:02 GMT, Content-Type=application/json}
Fri Dec 09 11:22:02 UTC 2016 : Method response body after transformations: {"statusCode":"200","headers":{"Content-Type":"application/json"},"body":"{\"message\":\"Cancelled appointment with id sfssdf\"}"}
Fri Dec 09 11:22:02 UTC 2016 : Method response headers: {X-Amzn-Trace-Id=Root=1-584a93da-f841704d9feb371b31e41cb9, Content-Type=application/json}
Fri Dec 09 11:22:02 UTC 2016 : Successfully completed execution
Fri Dec 09 11:22:02 UTC 2016 : Method completed with status: 200

所以现在看起来一切都很好,但是当我使用HTTP方法POST对来自PostMan的AWS API URL进行实际测试时,我得到了以null作为约会Id的响应,并且我可以在CloudWatch日志中看到没有在输入约会对象上设置appointmentId。

我觉得我错过了一些最基本的东西。如有任何帮助,将不胜感激。

更新

通过将lambda处理程序函数切换为使用Stream,而不是尝试将JSON序列化/反序列化为POJO来解决这个问题。当使用API Gateway Lambda代理时,处理程序的输入是一个复杂的JSON结构,我不想尝试将其复制为Java/Scala类。将输入作为流处理,将其解析为JsonObject,然后使用Gson或等效库将消息正文转换为POJO更容易。下面的示例处理程序,您还可以在这里看到一个更大的示例

class CancellationHandler {

  def cancelAppointment(request: InputStream, response: OutputStream, context: Context): Unit = {

    val logger = context.getLogger
    val parser: JsonParser = new JsonParser
    var inputObj: JsonObject = null
    val gson: Gson = new Gson

    try {

      inputObj = parser.parse(IOUtils.toString(request, "UTF-8")).getAsJsonObject

    } catch {

      case e: IOException =>

        logger.log("Error while reading request\n" + e.getMessage)
        throw new RuntimeException(e.getMessage)

    }

    val body: String = inputObj.get("body").getAsString
    val appointment: Appointment = gson.fromJson(body, classOf[Appointment])

    val apiGatewayProxyResponse = new ApiGatewayProxyResponse
    val cancelResponse = new CancelResponse

    cancelResponse.setMessage("Cancelled appointment with id " + appointment.getAppointmentId)

    apiGatewayProxyResponse.setBody(gson.toJson(cancelResponse))

    apiGatewayProxyResponse.setStatusCode("200")

    val headerValues = new util.HashMap[String, String]

    headerValues put("Content-Type", "application/json")

    apiGatewayProxyResponse.setHeaders(headerValues)

    val output: String = gson.toJson(apiGatewayProxyResponse)

    IOUtils.write(output, response, "UTF-8")
  }

}

共有1个答案

单于耘豪
2023-03-14

Lambda代理的输入形状将不同于常规非代理Lambda集成的形状。当然,这对于您的用例很重要,因为您使用的是Java/Scala,其中您必须显式地构造输入POJO。

代理输入如下所示:

{
  "resource": "\/pets",
  "path": "\/pets",
  "httpMethod": "POST",
  "headers": null,
  "queryStringParameters": null,
  "pathParameters": null,
  "stageVariables": null,
  "requestContext": {
    ...
    "stage": "test-invoke-stage",
    "requestId": "test-invoke-request",
    "identity": {
      ...
    },
    "resourcePath": "\/pets",
    "httpMethod": "POST"
  },
  "body": "{\n    \"foo\":\"bar\"\n}", <---- here's what you're looking for
  "isBase64Encoded": false
}

文档:http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html

 类似资料:
  • 我有一个用Java编写的AWS Lambda函数,它由AWS API网关调用触发。 我试图向其中一个endpoint发出请求,并使用JSON作为有效负载。 然后,网关将检测,并通过默认模板传递所有请求参数(包括正文)。有趣的是这一部分 它应该给我一张

  • 是否可以通过http请求(GET或POST)直接调用AWS Lambda函数?如果没有,是否有通过SNS或S3的解决方案?因为我想不出一个。。。 具体来说,我想使用 JSON 创建一个小的 API

  • 在AWS文档之后,我尝试使用以下http请求从api网关导出api定义。知道它为什么不起作用吗?

  • 首先,可以说这个问题非常类似于HTTP请求主体没有通过AWS API网关获得AWS lambda函数,或者通过API网关获得AWS lambda中的json主体 但是,这些问题都没有解决Golang的使用问题,我一直遇到的问题是找到一个与node.js文档中到处使用的参数相当的参数。 下面是我的Lambda函数: 车身:

  • 我制作了一个简单的JavaScript页面,当按下“发送”按钮时,该页面应该显示时间表并通过HTTP发送数据。 数据应该发送到一个Arduino使用这个简单的方案: http://arduinoip/DATASTARTSHEREhh:mm;hh:mm;hh:mm;[...];DATAENDSHERE 我使用以下代码打印我在Arduino串行监视器中收到的所有数据: 我收到的数据如下: 新客户 CM

  • 我对通过编程(通过HTTP POST或PUT请求)将文件(在我的例子中是图像或pdf)上传到Django应用程序的过程感到非常困惑。我读过医生的报告,但我必须承认它没有帮我度过难关。最让我困惑的是模板(和表单):为什么我需要在视图中指定模板和表单? 我只想将一个文件发送到一个目录,我想知道在Django部分和请求部分(内容类型…?)上执行此操作需要什么 我真的很感激任何人能在这里给我指明方向...