具有基本身份验证的Node.js http发布请求

时间:2018-12-26 14:32:39

标签: javascript node.js http

我可以使用axios进行此操作,但是由于某些原因,我想使用默认的http模块进行操作

这是代码

var express = require("express");
const http = require('https');
var app = express();

app.listen(3000, function () {
    var username = 'username';
    var password = 'password';
    var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');

const data = JSON.stringify({
    campaign_id: 'all',
    start_date: '01/01/2010',
    end_date: '05/31/2030',
    return_type: 'caller_view',
    criteria: {
        phone: 98855964562
    }
});

var hostName = "https://staging.crm.com";
var path = "/api/v1/caller_find";

const options = {
    hostName: hostName,
    path: path,
    port: 3000,
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': auth,
        'Content-Length': data.length
    }
};
const req = http.request(options, (res) => {
    console.log('response is ' + res);
});

req.on('error', (error) => {
    console.log('error is ' + error);
  });
});

但是它抛出异常

  

连接ECONNREFUSED 127.0.0.1:443

3 个答案:

答案 0 :(得分:1)

似乎您提供了错误的let obj = JSON.parse(result); let arrValues = Object.values(obj); 对象(可能是从axios复制过来的)。在提供options的同时,Node.js HTTP模块的选项包含hosthostname

参考:https://nodejs.org/api/http.html#http_http_request_options_callback

答案 1 :(得分:1)

我不确定您要做什么。为什么需要您的应用程序进行监听?我假设您要发布到的应用程序托管在其他地方,因为您尝试监听端口3000,同时还向端口3000上的应用程序发出请求。如果每个应用程序位于不同的主机上,则应精细。但是,您至少有3个问题。

1)您的选项对象不正确。您正在使用hostName的情况下使用hostname。这就是为什么您会收到ECONNREFUSED 127.0.0.1:443错误的原因; https.request()方法的options对象的默认主机名为localhost,端口默认为443。

2)而且,您永远不会将data对象的内容写入请求流。

3)最后,您应该收听data事件以获取响应并将其写入控制台。我已经更新了您的代码,如下所示:

var express = require("express");
const http = require('https');
var app = express();

app.listen(3000, function () {
  var username = 'username';
  var password = 'password';
  var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');

  const data = JSON.stringify({
      campaign_id: 'all',
      start_date: '01/01/2010',
      end_date: '05/31/2030',
      return_type: 'caller_view',
      criteria: {
          phone: 98855964562
      }
  });

  var hostName = "https://staging.crm.com";
  var path = "/api/v1/caller_find";

  const options = {
      hostname: hostName,
      path: path,
      port: 3000,
      method: 'POST',
      headers: {
          'Content-Type': 'application/json',
          'Authorization': auth,
          'Content-Length': Buffer.byteLength(data)
      }
  };

  const req = http.request(options, (res) => {
    console.log(`statusCode: ${res.statusCode}`)
    res.on('data', (chunk) => {
      console.log(`BODY: ${chunk}`);
    });
    res.on('end', () => {
      console.log('No more data in response.');
    });
  });

  req.on('error', (error) => {
    console.log('error is ' + error);
  });

  req.write(data);
  req.end();
});

答案 2 :(得分:-1)

您无法将Express应用程序原样移至AWS Lambda。有claudia之类的工具可以帮助您将应用程序移动到lambda和api网关。

根据您的情况,您可以按如下所示修改代码AWS Lambda

const http = require('https');

exports.myHandler = function (event, context, callback) {
var username = 'username';
var password = 'password';
var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');

const data = JSON.stringify({
    campaign_id: 'all',
    start_date: '01/01/2010',
    end_date: '05/31/2030',
    return_type: 'caller_view',
    criteria: {
        phone: 98855964562
    }
});

var hostName = "https://staging.crm.com";
var path = "/api/v1/caller_find";

const options = {
    hostName: hostName,
    path: path,
    port: 3000,
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': auth,
        'Content-Length': data.length
    }
};
const req = http.request(options, (res) => {
    console.log('response is ' + res);
    callback(null, res);

});

req.on('error', (error) => {
    console.log('error is ' + error);
    callback(error);
});
}

您必须通过API网关或其他AWS资源(例如Alexa Skill Kit等)来调用lambda。

编辑

您可以尝试按照@ https://github.com/request/request/blob/master/README.md#http-authentication

的指定传递身份验证选项