我正在使用普通的node.js脚本发出POST请求,并且我发出请求的链接大约需要1到1.2分钟来响应,并且在这段时间内出现此错误Error: socket hang up
。
大约在1到1.2分钟左右的时间里,我就收到了套接字挂起错误,与服务器的响应时间相同
我尝试在标头中设置连接:“ keep-alive”,并在选项中将超时设置为200000ms,但是没有运气。有帮助吗?
顺便说一句,如果我使用axios,该请求就可以正常工作
这是我的脚本
const fs = require("fs")
const path = require("path")
const https = require("https")
const file = fs.createWriteStream(path.join(__dirname, "data.csv"))
const options = {
hostname: "example.com",
path: "/example",
method: "POST",
headers: {
<<headers>>
}
<<some other options>>
}
const req = https.request(options, response => {
response.on("data", function (chunk) {
console.log("started...")
chunk.pipe(file)
})
response.on("end", function () {
file.on("finish", function () {
file.close()
})
})
})
req.on("error", e => {
console.error(e)
})
req.end()
这是完整的错误:
Error: socket hang up
at connResetException (internal/errors.js:612:14)
at TLSSocket.socketOnEnd (_http_client.js:493:23)
at TLSSocket.emit (events.js:326:22)
at endReadableNT (_stream_readable.js:1308:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'ECONNRESET'
}
答案 0 :(得分:0)
发现我犯的错误
第一,而不是在chunk.pipe(file)
内进行response.on('data')
我直接做了response.pipe(file)
,但确实有效,我不知道为什么,我猜是因为响应是流,而块只是缓冲区,如果有人知道,请告诉我。
第二我在选项内设置了请求正文,我必须设置req.write(formData)
这样的请求正文参数,其中formData是我要作为请求正文发送的字符串。 / p>