RafDev RafDev - 4 months ago 22
Node.js Question

Make HTTP GET request using Node.js

I'm trying to make an HTTP GET request using Node.js but it errors


Request to '/path/to/file?query=string' failed: read ECONNRESET


How may I fix it? Thanks in advance. It was working before, I'm not sure of what changed.

var http = require("http");
var data = "";

var options = {
host: "sub.example.com",
port: 80,
path: "/path/to/file?query=string",
method: "GET",
};

var req = http.request(options, function(resp){
resp.on('data', function(_data){data = data + _data});
resp.on('end', function(){callback(data)})
});

req.on('error',function(e){
console.log("Request to '" + filename + "' failed: " + e.message)
});

req.write('data\n');
req.write('data\n');
req.end();

Answer

GET requests typically do not have a body. Try removing the req.write() lines and you may have better luck.

Comments