request: Error: socket hang up
Hi, i know that such issues were created before, but there was a little bit different description.
I tried to perform simple request: send form data. But when i use request module it always throw “socket hang up”.
Below simple test case and results
'use strict';
const request = require('request');
const http = require('http');
const querystring = require('querystring');
const data = {
xstext: 'I have a some problem about node.js server. What should I do to solve the this problem?',
spintype: 0,
removeold: 0,
};
// Using request
request(
{
method: 'POST',
url: 'http://address:9017/',
form: data,
},
(error, responce, body) => {
if (!error) {
console.log(body, responce);
return;
}
console.log(error);
}
);
// Using native http
let postData = querystring.stringify(data);
let options = {
hostname: 'address',
port: 9017,
path: '/',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length,
},
};
let req = http.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
});
req.write(postData);
req.end();
For request module:
{ [Error: socket hang up] code: 'ECONNRESET' }
For native http:
STATUS: 200
HEADERS: {"content-length":"98","content-type":"text/html","cache-control":"no-cache","connection":"keep-close"}
BODY: Excellent some problem about client. js server. What must i do to solve the particular this issue?
No more data in response.
Loks like it’s really bug in request module.
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 6
- Comments: 84
Commits related to this issue
- Change requests to have keep-alive in headers As referred to https://github.com/request/request/issues/2047#issuecomment-272473278 — committed to udacimak/udacimak by udacimak 6 years ago
- Resolution to #1095 According to the advice given in the thread https://github.com/request/request/issues/2047 — committed to kika/firebase-tools by kika 5 years ago
- see #23; seeing if slashes help at all: https://github.com/request/request/issues/2047#issuecomment-275304197 — committed to ubccpsc310/classy by rtholmes 5 years ago
I have same problem, but add option
gzip:truewill work.Somebody save me 👼
In my case I was able to workaround the problem by setting the
Connection: keep-aliveheader.I see people are getting this error more frequently. In my case it was really simple. What I though was a properly formed URL based on one of my algorithms, was actually malformed. I encourage all of you experiencing this issue to assume the problem is in your code. I assure you, in 98% of the cases the root cause is something mundane that you are taking for granted or overlooking. I have tried all of the above to solve the problem, but in the end, it came down to an extra forward slash.
Wget, curl - success, but request - error.
Node - v6.9.4 Request - 2.79.0
I don’t think that it happens because of the bug you pointed. In our case it appears :
I’m confused to get this error because in a usual case, our backend server should throw this error or?
NOTE: recording this for others who will probably stumble over this discussion when searching the internet for that particular error message.
As it turns out most of the discussion above misses the point, because the error is not caused by the client site and hence trying different “workarounds” for
requestwill most likely not work. The problem is that the HTTP request socket on the server side runs into the idle timeout, because of a long delay in a background operation, and then closes the socket. That results in the ECONNRESET on the client side.If you are also responsible for the server side source code and use the express/koa framework then you should disable the idle timeout on the HTTP request socket before initiating the long lasting backend operation. E.g. for an ES6 koa route handler:
Now my client timeout problem is gone without changing any line in the client code…
Please try request to google.com => hostname: ‘google.com’ to check it work or not. In my case, “hostname” was handle by nginx as proxy and it does not response when receive request with empty body. This code get error:
This code works
similar issue in native https.request issue see more at here,
add below to request option will bypass (fix) this issue,
i got caught up and spent the whole afternoon trying to figure it out… was in a complicated environment with several layers of redirect:
is this DNS issue? -> no way it's DNS -> definitely DNS -> no it's nottill i saw @fractalf 's example, and realized that all these happened only to sites hosted on IIS6 with httpsUsing ‘http’ instead of ‘https’ protocol solve the issue too.
We ran into this issue after upgrading to a newer nodejs (
10.16.3). On client side we used nodejs http agent withkeepAlive: true. It looks like the following happened:Solution So far we’ve found two solutions:
keepAliveTimeoutby setting it equal to0keepAliveTimeoutvalue (by default it 5 seconds). The default http agent does not support such a capability (timeoutsetting does not do it). So we used agentkeepalive libI have the same bug. The POST works when Content-Length is added to the request. Hope this helps you guys: ‘Content-Length’: Buffer.byteLength(data) //Content-Length is needed for a POST
I’m seeing this too with node 6.6.0. The server I’m connecting to is not sending a Content-Length header in the response, in which case according to the HTTP spec the server should close the stream after sending all data. I suspect this is being misinterpreted as a socket hang up. Making the same request with cURL works fine.
happening with Node v6.9.2.
try remove ‘accept-encoding’: ‘gzip, deflate’ in the headers solved this.
i have the same issue when i’m testing my routes with chai-http. the absurd is that the first three pass ok!! i’m try to face it all day
here my code:
Same issue, node v9.5.0
npm configuration:
EDIT : I was missing
req.end(). WorksFor me, this is happening while sending a
POSTrequest withcontent-length : 0in the request headers. I don’t know if this result is reasonable, but at least I think maybe you can try removing each item in headers you added to find out the reason for the problem.I am still getting this problem.
Node.js 6.10.0 request 2.72.0
We found the same issue while running some backend integration tests. Our product connects to several network devices using HTTPS. In this scenario, we are establishing several HTTPS connections and executing some POST and GET requests to each device. Since the problem occurred in our test environment, the workaround was to disable the HTTP persistent connections (set HTTP header
Connection: close). By doing this, we ask the server to close the connection after delivering the response (HTTP/1.1). However, we lose the benefits of HTTP persistent connections since now each TCP will serve only one request.+1 Node 6.9.1 request 2.79.0
I have seen this same problem with HapiJS. The issue was a wrong content-length header
Any update about this?. I see the error with botkit 0.2.1.
Try this.
+1 Node 6.9.1
This only happens every now and then, when I test my endpoints locally, running many requests in a rather fast manner…
make sure you’re calling the url with the right SSL - either https or http
Finally got this to work for the recaptcha. I had to import and use the https library. Here is the working code:
var https = require(‘https’); var querystring = require(‘querystring’);
var secret = “YOUR_KEY”; var response = RESPONSE_CODE; var postData = “secret=”+secret+“&”+“response=”+response;
// Build the post string from an object var post_data = querystring.stringify({ ‘compilation_level’ : ‘ADVANCED_OPTIMIZATIONS’, ‘output_format’: ‘json’, ‘output_info’: ‘compiled_code’, ‘warning_level’ : ‘QUIET’, ‘js_code’ : postData });
var options = { hostname: ‘www.google.com’, port: 443, path: ‘/recaptcha/api/siteverify’, method: ‘POST’, headers: { ‘Content-Type’: ‘application/x-www-form-urlencoded’ //‘content-encoding’: ‘gzip’, //‘Connection’: ‘close’ }, agentOptions: { ciphers: ‘DES-CBC3-SHA’ }
}; var req = https.request(options, function(res) { console.log('Status: ’ + res.statusCode); console.log('Headers: ’ + JSON.stringify(res.headers)); res.setEncoding(‘utf8’); res.on(‘data’, function (body) { console.log('Body: ’ + body); }); }); req.on(‘error’, function(e) { console.log('problem with request: ’ + e.message); }); // write data to request body req.write(postData); req.end();
For me, curl is not working. I found i had set proxy to my command line tool before. It works fine now when i remove proxy.
No luck.
setting ciphers to following didn’t work for me. agentOptions: { ciphers: ‘DES-CBC3-SHA’ }
Hi Guys, I have the same issue. +1 Luckily I use ‘request’ within a 2nd monitoring tool so my issues are minimal. I like node.js a lot, so I’m posting some feedback. I hope my feedback helps a little with your attempts to fix the issue.
The error is now appearing consistently in a high traffic production environments. I can spin up an “exact” replica of the server and the error “never” appears. I can switch endpoints around, and it always ends up the high traffic production environment causes this error for the client. { [Error: socket hang up] code: ‘ECONNRESET’ }.
This began as an intermittent error that I noticed several days ago and after the monitoring service false alarmed too many times… and a heck of a lot of times testing with production test tools that doesn’t use ‘request’ I began searching the web to find this issue here.
Now I have not changed any code/version in over 8 months for this application and am confused to how this can happen out of no where. So… what changed in the ‘request’ world if I didn’t change any client code?
+1 Node 6.9.5
i try to follow all suggest that you say, but this issue does still happen. so sad…