2015-08-17 147 views
4

我搜索了AWS Lambda文档,但找不到我的问题的答案。AWS Lambda Payloads

有没有一种方法可以从Lambda函数(用node.js编写)访问整个请求体?

event参数似乎只包含解析的JSON属性。

回答

2

您的请求正文需要使用XML或JSON以便能够在您的Lambda函数中访问它。您需要指定它在API Gateway仪表板的Integration Request部分中的处理/映射和传递方式。

+0

您可以在不使用API​​网关的情况下自定义请求参数吗? – user2976753

+0

你将如何执行Lambda函数? – kixorz

+0

AWS CloudWatch内部的安排 – user2976753

0

作为替代方案,如果它更适合您的用例,您可以考虑将lambda设置为简单代理。我发现最近有更多人使用这种技术。

http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html

请求等以下几点:

POST /testStage/hello/world?name=me HTTP/1.1 
Host: gy415nuibc.execute-api.us-east-1.amazonaws.com 
Content-Type: application/json 
headerName: headerValue 

{ 
    "a": 1 
} 

会倒闭以下事件数据发送到您的AWS lambda表达式:

{ 
    "message": "Hello me!", 
    "input": { 
    "resource": "/{proxy+}", 
    "path": "/hello/world", 
    "httpMethod": "POST", 
    "headers": { 
     "Accept": "*/*", 
     "Accept-Encoding": "gzip, deflate", 
     "cache-control": "no-cache", 
     "CloudFront-Forwarded-Proto": "https", 
     "CloudFront-Is-Desktop-Viewer": "true", 
     "CloudFront-Is-Mobile-Viewer": "false", 
     "CloudFront-Is-SmartTV-Viewer": "false", 
     "CloudFront-Is-Tablet-Viewer": "false", 
     "CloudFront-Viewer-Country": "US", 
     "Content-Type": "application/json", 
     "headerName": "headerValue", 
     "Host": "gy415nuibc.execute-api.us-east-1.amazonaws.com", 
     "Postman-Token": "9f583ef0-ed83-4a38-aef3-eb9ce3f7a57f", 
     "User-Agent": "PostmanRuntime/2.4.5", 
     "Via": "1.1 d98420743a69852491bbdea73f7680bd.cloudfront.net (CloudFront)", 
     "X-Amz-Cf-Id": "pn-PWIJc6thYnZm5P0NMgOUglL1DYtl0gdeJky8tqsg8iS_sgsKD1A==", 
     "X-Forwarded-For": "54.240.196.186, 54.182.214.83", 
     "X-Forwarded-Port": "443", 
     "X-Forwarded-Proto": "https" 
    }, 
    "queryStringParameters": { 
     "name": "me" 
    }, 
    "pathParameters": { 
     "proxy": "hello/world" 
    }, 
    "stageVariables": { 
     "stageVariableName": "stageVariableValue" 
    }, 
    "requestContext": { 
     "accountId": "12345678912", 
     "resourceId": "roq9wj", 
     "stage": "testStage", 
     "requestId": "deef4878-7910-11e6-8f14-25afc3e9ae33", 
     "identity": { 
     "cognitoIdentityPoolId": null, 
     "accountId": null, 
     "cognitoIdentityId": null, 
     "caller": null, 
     "apiKey": null, 
     "sourceIp": "192.168.196.186", 
     "cognitoAuthenticationType": null, 
     "cognitoAuthenticationProvider": null, 
     "userArn": null, 
     "userAgent": "PostmanRuntime/2.4.5", 
     "user": null 
     }, 
     "resourcePath": "/{proxy+}", 
     "httpMethod": "POST", 
     "apiId": "gy415nuibc" 
    }, 
    "body": "{\r\n\t\"a\": 1\r\n}", 
    "isBase64Encoded": false 
    } 
} 

现在,您可以访问所有的头,url params,body等在每个请求中。

1

您可以在AWS Lambda中访问正文映射模板中的请求正文。

  1. 在API网关控制台打开你的方法
  2. 打开Integration Request
  3. Integration Request,打开Body Mapping Templates面板
  4. 添加Content Typeapplication/json
  5. 一个点击你刚创建的application/json项目
  6. 添加以下模板:
{ 
    "body" : $input.json('$') 
} 

之后,你可以访问请求主体作为你的Node.js lambda函数event.body