简体   繁体   中英

NodeJS HTTP GET returns empty body

Here's a NodeJS code snippet I'm debugging. The url is correct, and a GET request to it with Postman returns a 200 OK with the following headers. The response body is a valid JSON string.

Headers (arrows represent column divisors)

Accept-Ranges →bytes
Age →0
Cache-Control →max-age=300
Connection →keep-alive
Content-Encoding →gzip
Content-Length →255
Content-Type →application/json
Date →Tue, 24 Jan 2017 22:37:28 GMT
Expires →Tue, 24 Jan 2017 17:47:43 GMT
Last-Modified →Tue, 24 Jan 2017 01:03:08 GMT
Server →nginx
Vary →Accept-Encoding
Via →1.1 varnish
X-Cache →HIT
X-Cache-Hits →1
X-Served-By →cache-yul8926-YUL
X-Timer →S1485297448.259768,VS0,VE89

The problem:

I've discovered that res.on('data', function(chunk) { never gets called which causes body to remain empty.

When res.on('end', function() { is called body still has length = 0. Any ideas why the data callback is not getting called?

http.get(url, function(res) {
    var body = '';

    res.on('data', function(chunk) {
      body += chunk;
    });

    res.on('end', function() {
      var data = JSON.parse(body);
      cb(data, undefined);
    });
}).on('error', function(err) {
  console.log("Something went wrong");
  console.log(err);
});

Also worth nothing, cb is a callback function defined outside of this snippet.

After the lovely advice from jfriend00 I discovered that res.statusCode was 301 (Moved Permanently). Seems that the website was redirecting to the same URL but with https protocol instead of http .

When testing the REST API with Postman it did not mention the fact the request was redirected. Postman displayed a 200 OK response (???) According to the Postman Docs it will default to following redirects silently.

The solution:

I replaced all instances of http with https and it worked great.

https.get(url, function(res) {
    var body = '';

    res.on('data', function(chunk) {
      body += chunk;
    });

    res.on('end', function() {
      var data = JSON.parse(body);
      cb(data, undefined);
    });
}).on('error', function(err) {
  console.log("Something went wrong");
  console.log(err);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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