2015-08-17 144 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