Home  >  Article  >  Web Front-end  >  Nodejs Post request reports socket hang up error solution_node.js

Nodejs Post request reports socket hang up error solution_node.js

WBOY
WBOYOriginal
2016-05-16 16:35:372265browse

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):

Copy code The code is as follows:

var querystring = require('querystring')
, http = require('http');

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn