Aws LAMBDA:HTTP POST 请求

Aws LAMBDA : HTTP POST request

我正在尝试 post 来自 OData RESTful API 的数据,来自使用的示例服务,即 TripPin。 请求成功但响应为空。为了发出 HTTP post 请求,AWS Lambda 平台是否有任何特定结构需要遵守?

var querystring = require('querystring');
var http = require('http');

exports.handler = function (event, context) {
var post_data = querystring.stringify(
     {
    UserName:'lewisblack',
    FirstName:'Lewis',
    LastName:'Black',
    Emails:[
        'lewisblack@example.com'
    ],
    AddressInfo:[
        {
            Address: '187 Suffolk Ln.',
            City: {
CountryRegion: 'United States',
Name: 'Boise',
Region: 'ID'
            }
        }
    ],
    Gender: 'Male'
}
  );


  // An object of options to indicate where to post to
  var post_options = {
      host: event.url,
      port: '80',
      path: event.path,
      method: 'POST',
      headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Content-Length': Buffer.byteLength(post_data)
      }
  };

  // Set up the request
  var post_req = http.request(post_options, function(res) {
      res.setEncoding('utf8');
      res.on('data', function (chunk) {
          console.log('Response: ' + chunk);
          console.log("hello");
          context.succeed();
      });
      res.on('error', function (e) {
        console.log("Got error: " + e.message);
        context.done(null, 'FAILURE');
      });

  });

  // post the data
  post_req.write(post_data);
  post_req.end();

}

调用参数示例:

{
  "url": "services.odata.org",
  "path": "/v4/TripPinServiceRW/"
}

回复: 空

请求 ID: “6f1ec2b4-5195-477f-9fb8-56fd33dee0ce”

函数日志: 开始 RequestId:6f1ec2b4-5195-477f-9fb8-56fd33dee0ce 版本:$LATEST

END RequestId: 6f1ec2b4-5195-477fenter code here-9fb8-56fd33dee0ce

REPORT RequestId:6f1ec2b4-5195-477f-9fb8-56fd33dee0ce 持续时间:431.87 毫秒

计费持续时间:500 毫秒内存大小:128 MB 使用的最大内存:73 MB

TL;DR; 认为 您的 AWS Lambda 集成已损坏,但我很难确定。为演讲道歉。

我推荐以下内容:

弄清楚您在活动中得到了什么

您在 event 对象中获得的内容取决于您调用 AWS Lambda 函数的方式。您可以 invoke it directly via an API call,或通过其他 AWS 服务间接触发它。注意:

The structure of the event document is different for each event type, and contains data about the resource or request that triggered the function

https://docs.aws.amazon.com/lambda/latest/dg/lambda-services.html

找出答案的最简单方法可能是在处理程序的开头添加一些日志记录:

console.log(JSON.stringify(event));

写一些测试!

一旦您弄清楚 event 对象中的实际内容,为其编写一个测试。这应该会改善您的代码设计和开发周期。

针对 local test server 测试上面的代码给出了成功的响应。见下文。

但这并不意味着它会对抗 https://services.odata.org

  • 那个 api 看起来像是只读的,但您正试图写入它。
  • 您正在通过端口 80 上的 HTTP 调用它。看起来它想通过端口 443 上的 HTTPS 调用它。

同样,您应该弄清楚应该如何调用该服务并为其编写测试。

示例测试用例

index.spec.js

var index = require("./index");

describe('index', () => {
    it('can make requests to localhost', (done) => {
        return index.handler({
            "url": "localhost",
            "path": "/"
          }, {
              done: done,
              succeed: done
        });
    });
});

package.json

  ...
  "scripts": {
    "test": "jest"
  },
  "devDependencies": {
    "jest": "^24.8.0"
  }
  ...

输出:

> npm test

  > lambda-post@1.0.0 test /somepath/index.spec.js
  > jest

   PASS  ./index.spec.js
    index
      ✓ can make requests to localhost (20ms)

    console.log index.js:44
      Response: <html><body><h1>POST!</h1></body></html>

    console.log index.js:45
      hello

  Test Suites: 1 passed, 1 total
  Tests:       1 passed, 1 total
  Snapshots:   0 total
  Time:        0.89s, estimated 1s
  Ran all test suites.

使用更简单的 http 库

您正在使用低级节点 http 库。像 axios 这样的图书馆可能 在短期内让您的生活更轻松。请参阅自述文件/wiki 上的 HTTP Post 示例。