【发布时间】:2017-07-04 16:00:53
【问题描述】:
我正在尝试处理从 ASP.NET WEB API 发送到 AWS API 的请求并使用 Lambda 函数对其进行处理:
这是我调用部署在 AWS 中的 REST API 的方式:
// Serialize our concrete class into a JSON String
var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(payload));
// Wrap our JSON inside a StringContent which then can be used by the HttpClient class
var httpContent = new StringContent(stringPayload,Encoding.UTF8,"application/json");
using (var httpClient = new HttpClient()) {
// Do the actual request and await the response
var httpResponse = await httpClient.PostAsync("https://hosteddress-2.amazonaws.com/prod/SlashcmdIntegeration", httpContent);
// If the response contains content we want to read it!
if (httpResponse.Content != null) {
var responseContent = await httpResponse.Content.ReadAsStringAsync();
// From here on you could deserialize the ResponseContent back again to a concrete C# type using Json.Net
}
}
这是在 aws lambda 中处理它的代码:
var AWS = require('aws-sdk');
var qs = require('querystring');
var https=require('https');
var token;
exports.handler = function (event, context) {
processEvent(event, context);
};
var processEvent = function(event, context) {
var body = event.body;
var params = qs.parse(body);
var commandText = params.CommandText;
var arr = commandText.split(" ");
var op1 = arr[0];
var op2 = arr[1];
var op = arr[2];
var result;
switch(op) {
case "+":
result = Number(op1) + Number(op2);
break;
case "-":
result = Number(op1) - Number(op2);
break;
case "*":
result = Number(op1) * Number(op2);
break;
case "/":
result = Number(op2)===0 ? NaN : Number(op1) / Number(op2);
break;
default:
result = "Invalid op";
}
console.log('data sent');
context.succeed(result);
};
我尝试调试它 var body = event.body; 未定义,但我已在集成请求中设置它并最终登陆到以下错误消息:
{ "errorMessage": "进程在完成请求前退出" }
请问有什么想法吗?
谢谢
【问题讨论】:
-
没有得到这个
var body = event.body; it not undefined,你的请求中有正文吗? -
@AJS 。这就是我认为的问题,event.body 是未定义的,但是它应该得到我在 AWS API 的集成请求中正确设置的主体。
-
这个错误意味着api请求失败,因为它没有发送任何响应。
标签: node.js asp.net-web-api aws-lambda aws-api-gateway