Home > Article > Web Front-end > Nodejs Post request reports socket hang up error solution_node.js
Refer to the method of sending http post request on nodejs official website, and implement a function to simulate post submission. In actual use, a socket hang up error occurs.
Later it was discovered that it was a problem with the request header settings. The headers field information needs to be added to the sending options (this is probably related to the other party's server, and the incomplete post request headers may be discarded).
The complete code is as follows (students who encounter type problems can use it as a reference):
var data = querystring.stringify({
info:'hi',
test:5
});
var opt = {
hostname:'www.test.com',
port:9094,
path:'/perationSqlQuery',
method: 'POST',
headers: {
'Content-Type':'application/x-www-form-urlencoded',
'Content-Length': data.length
}
};
var req = http.request(opt, function (res) {
res.on('data', function (data) {
console.log(data.toString());
});
});
req.on('error', function(e) {
console.log('problem with request: ' e.message);
});
req.write(data);
req.end();