【问题标题】:How to process the JSON request sent from ASP.NET WEB API to AWS Lambda using node.js如何使用 node.js 处理从 ASP.NET WEB API 发送到 AWS Lambda 的 JSON 请求
【发布时间】: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


【解决方案1】:

从上面的代码中我可以理解

{ "errorMessage": "进程在完成请求前退出" }

当您的进程无法发送响应时会发生此错误,因为在它可以这样做之前发生了一些错误。我建议您在开始像这样解析它之前检查正文

var processEvent = function(event, context) {
  var body = event.body;
  var result;
  if (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];

    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);
};

至于导致错误的原因我不太确定,因为我没有使用过 lambda,这纯粹是从 nodejs 的角度来看。

【讨论】:

  • 如果我发现任何可以帮助与 lamda 相关的特别有用的信息,我会更新答案
  • 在 API 的集成请求中,我将正文映射模板指定为:{"body" : $input.json('$') },事件在 node.js 处理程序中不接受它。我刚刚尝试过像事件这样的直接调用。[参数名称]正在工作:)
  • 我发现这是一个不错且干净的实现,可以试试这个。aws.amazon.com/blogs/compute/…
  • 这种方法感觉更实用,因为它可以让您拥有多个端点,而不仅仅是一个功能,而且还可以为您做很多工作。我没有使用过 lamda,所以我可能是错的,只是谈谈节点的经验:)
【解决方案2】:

直接从事件调用参数是有效的:

 var processEvent = function(event, context) {
      var body = event.CommandText;/*parameter name*/
      var result;
      if (body) {


        var arr = body.split(" ");
        var op1 = arr[0];
        var op2 = arr[1];
        var op = arr[2];

        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);
    };

只是想知道身体映射模板的使用。

【讨论】:

    猜你喜欢
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 2021-11-06
    • 2018-01-28
    • 2020-06-27
    相关资源
    最近更新 更多