Reputation: 187
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.
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
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.
Upvotes: 3
Views: 2379
Reputation: 187
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 According to the Postman Docs it will default to following redirects silently.200
OK response (???)
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);
});
Upvotes: 2