简体   繁体   English

节点JS HTTP请求不使用http请求执行任何操作

[英]node JS HTTP request does nothing using http request

I'm trying to send http post request using native node js http request. 我正在尝试使用本机节点js http请求发送http post请求。 I'm using the following code but nothing happens: 我正在使用以下代码,但没有任何反应:

    var http = require('http');

    var options = {
      hostname: '192.168.1.134',
      port: '8082',
      path: '/api',
      method: 'POST',
      headers: {'content-type': 'application/json',
      'cache-control': 'no-cache'}
    };

    callback = function(response)
    {
      var result = [];
      response.on('data', function (chunk)
      {
        result.push(chunk);
      });

      response.on('end', function ()
      {
        console.log("LOCAL END" + result);
      });
    }

    var req = http.request(options, callback);


    req.write(JSON.stringify(
    { 
        customer: 'customer',
        deviceIndicator: 'id',
        userId: 'id2',
        lastVersion: 999 
    }), 'utf8' , 
    function(data)
    {
        console.log('flushed: ' + data);
    });
    req.end();

    console.log(" - trying to post to example - done" );

But if i'm adding the following dummy calls i'm getting an answer from my local server as expected: 但是,如果我要添加以下虚拟呼叫,那么我将从本地服务器得到预期的答复:

    var options1 = {
      hostname: 'www.google.com',
      port: '80',
      path: '/',
      headers: {'cache-control': 'no-cache'}
    };

    callback1 = function(response1) {
      var str = ''
      response1.on('data', function (chunk) {
        str += chunk;
      });

      response1.on('end', function () {
        console.log("GOOGLE END" + str);
      });
    }

    var req1 = http.request(options1, callback1);
    req1.end();

    console.log("sent to google - done");

What am i doing wrong? 我究竟做错了什么?

Make sure 192.168.1.134:8082 is reachable and responding (using a browser, curl or wget) then try adding a content-length header: 确保192.168.1.134:8082可以访问并且响应(使用浏览器,curl或wget),然后尝试添加content-length标头:

var http = require('http');

var payload = JSON.stringify({ 
  customer: 'customer',
  deviceIndicator: 'id',
  userId: 'id2',
  lastVersion: 999 
});

var options = {
  hostname: '192.168.1.134',
  port: 8082,
  path: '/api',
  method: 'POST',
  headers: {
    'content-length': Buffer.byteLength(payload), // <== here
    'content-type': 'application/json',
    'cache-control': 'no-cache'
  }
};

var req = http.request(options, function(response) {
  var result = [];

  response.on('data', function(chunk) {
    result.push(chunk);
  });

  response.on('end', function () {
    console.log('LOCAL END' + result);
  });
});

req.write(payload);
req.end();

Eventually, I discovered that the problem was with the device itself which had some kind of problem.. When sent http request to a direct ip address nothing happened but when sent to an address that need dns server it is working... 最终,我发现问题出在设备本身上,它有某种问题。.当将http请求发送到直接ip地址时,什么都没有发生,但是当发送到需要dns服务器的地址时,它却在工作...

Unfortunately, I don't have any additional info about this bug... 不幸的是,我没有关于此错误的任何其他信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM