当我通过邮递员测试api网关端点时,收到200响应,在正文中发送json。
像这样在lambda中读取主体:
const user = JSON.parse(event.body);
但是,当我想使用测试事件通过控制台测试lambda时,总是会遇到运行时错误。
{ "errorType": "SyntaxError", "errorMessage": "Unexpected token u in JSON at position 0", "trace": [
"SyntaxError: Unexpected token u in JSON at position 0",
" at JSON.parse (<anonymous>)",
" at Runtime.module.exports.func [as handler] (/var/task/createUser.js:10:23)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)",
" at process._tickCallback (internal/process/next_tick.js:68:7)" ]
我已经在测试事件中尝试了json的字符串化+转义版本和一个普通的json对象
"{\"email\":\"jane.smith@enterprise.co.uk\",\"givenName\":\"Jane\",\"familyName\":\"Smith\",\"regionId\":\"59BCCF09-9ADD-8876-6FA6-8404B30E7B80\",\"userRoles\":[{\"establishmentId\":\"59BCCF09-9ADD-8876-6FA6-8404B30E7B80\",\"role\":\"admin\"}]}"
唯一有效的方法就是不解析代码,这既麻烦又不可行。
const user = event.body;
因为Web api测试现在失败了!