使用 Node.js 发出 HTTP GET 请求

Make HTTP GET request using Node.js

本文关键字:GET 请求 HTTP 发出 Node js 使用      更新时间:2023-09-26

我正在尝试使用Node发出HTTP GET请求.js但它错误

对"/path/to/file?query=string"的请求失败:读取 ECONNRESET

我该如何解决?提前谢谢。它以前在工作,我不确定发生了什么变化。

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();

GET请求通常没有正文。尝试删除req.write()行,您可能会有更好的运气。