AWS Lambda callback response format is not correct
See original GitHub issueI’m using a basic Hello World app to test the Actions SDK v2.0.1 in the Simulator using Bespoken Tools’ Lambda proxy. Note that I had to use node-mocks-http to build a fake event object similar to what the SDK’s Lambda framework expects from an API Gateway event.
The Lambda callback is incorrect in this scenario, leading to a parse error when the Assistant service receives the response from the fullfillment app.
'use strict';
const {actionssdk} = require('actions-on-google');
const expressMockery = require("node-mocks-http");
const app = actionssdk({debug: true});
// intent handler
app.intent('actions.intent.MAIN', (conv) => {
conv.close('Hello world!');
});
// When using API Gateway it should be as easy as this, but it doesn't work like that with BST proxy
// exports.handler = app;
exports.handler = async (event, context, callback) => {
try {
// mock an API Gateway request
let request = expressMockery.createRequest({
body: JSON.stringify(event),
headers: []
});
await app(request, context, callback);
} catch(err) {
callback(err);
}
}
The Actions Console Simulator returns the following error:
UnparseableJsonResponse API Version 2: Failed to parse JSON response string with 'INVALID_ARGUMENT' error: "body: Cannot find field.".
As far as I can tell, it is saying that the body
field in the repsonse does not exist in the expected response schema.
The Problem appears to be caused how the Lambda framework in the SDK calls the callback. Current implementation (in src/framework/lambda.ts
):
const { status, body } = result
callback(null, {
statusCode: status,
body: JSON.stringify(body),
})
There is a trivial fix for this but I’m reluctant to post it here until I can establish my employer’s legal position. There is a consensus that raising the issue is acceptable.
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (3 by maintainers)
Top GitHub Comments
The current lambda framework implementation builtin to the library expects the handler to be from lambda’s API Gateway. We can explore being able to customize it in the future.
For now, you don’t have to necessarily mock the request as long as you know how to extract the JSON body and headers.
Just call
app.handler
which is aStandardHandler
and is the most basic handler that’s not dependent on any framework.It accepts a JSON body as the first argument, a JSON map of header key to header values (as a string or string array), and returns a
Promise
with the result as aStandardResponse
which contains the responsebody
as a JSON and thestatus
as a number.Using this method, you should be able to use whatever input and output format you like.
No.I have not tried. Is this method working for app?