【问题标题】:How to POST data to an HTTP endpoint using NodeJs如何使用 NodeJs 将数据发布到 HTTP 端点
【发布时间】:2017-07-05 11:29:50
【问题描述】:

我是 AWS Lambda 的新手
我试图将 AWS 数据中的简单数据发布到 HTTP 端点,但我无法做到这一点。
我使用的是 NodeJs 语言。 我希望使用 http 模块而不是使用 request 模块来完成 POST 方法。 如何解决这个问题。

【问题讨论】:

  • 任何具体错误,显示您尝试过的内容

标签: node.js http http-post aws-lambda


【解决方案1】:

如果你真的只想使用http模块,你可以从这个例子开始:

const http = require('http');

const requestBody = 'your=post&content=here';
const options = {
    host: 'httpbin.org',
    path: '/post',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Content-Length': requestBody.length
    }
};

const req = http.request(options, (res) => {
    var responseString = '';

    res.on('data', function (data) {
        responseString += data;
        // save all the data from response
    });
    res.on('end', function () {
        console.log(responseString);
        // print to console when response ends
    });
});
req.write(requestBody);
req.end();

【讨论】:

    【解决方案2】:

    我假设您想使用本机 HTTP 模块,因为您不知道如何包含外部模块。由于可能是这种情况,请花时间阅读 AWS Creating a Deployment Package (Node.js) 的这篇文章。

    它应该可以帮助您了解如何使用 AWS Lambda、NodeJS 和外部模块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 2023-01-29
      相关资源
      最近更新 更多