I am making a post request using request package. how can I set timeout 500 ms while requesting.
var request = require('request');
request({
url: 'myUrl',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'authToken': 'myToken'
},
timeout: 500,
json: infoObj
})
.on('response', function(response) {
console.log('request response=============',response.statusCode)
})
.on('error', function(err) {
console.log('error===',err);
});
when tried using timeout: 500, above example got error
{ Error: ETIMEDOUT at Timeout._onTimeout (F:\gomean\globeone\node_modules\request\request.js:796:17) at tryOnTimeout (timers.js:224:11) at Timer.listOnTimeout (timers.js:198:5) code: 'ETIMEDOUT', connect: true }
I am making a post request using request package. how can I set timeout 500 ms while requesting.
var request = require('request');
request({
url: 'myUrl',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'authToken': 'myToken'
},
timeout: 500,
json: infoObj
})
.on('response', function(response) {
console.log('request response=============',response.statusCode)
})
.on('error', function(err) {
console.log('error===',err);
});
when tried using timeout: 500, above example got error
Share Improve this question edited Nov 3, 2016 at 7:44 Shaishab Roy asked Nov 3, 2016 at 7:13 Shaishab RoyShaishab Roy 16.8k7 gold badges55 silver badges69 bronze badges 8{ Error: ETIMEDOUT at Timeout._onTimeout (F:\gomean\globeone\node_modules\request\request.js:796:17) at tryOnTimeout (timers.js:224:11) at Timer.listOnTimeout (timers.js:198:5) code: 'ETIMEDOUT', connect: true }
- Can you tell us what are you trying to achieve? – Rajesh Commented Nov 3, 2016 at 7:16
-
request({ ..., timeout: <milliseconds> })
– Andreas Commented Nov 3, 2016 at 7:18 - updated question that i tried – Shaishab Roy Commented Nov 3, 2016 at 7:29
- Your error looks like what you get when you get an unhandled timeout error. I don't see any error handler in your code that would catch errors, including the timeout error. – jfriend00 Commented Nov 3, 2016 at 7:37
- node engine version? – Kamal Commented Nov 3, 2016 at 7:40
2 Answers
Reset to default 1You need to include a callback to catch the error. From the documentation:
You can detect timeout errors by checking err.code for an 'ETIMEDOUT' value. Further, you can detect whether the timeout was a connection timeout by checking if the err.connect property is set to true.
And an example:
request.get('http://10.255.255.1', {timeout: 1500}, function(err) {
console.log(err.code === 'ETIMEDOUT');
// Set to `true` if the timeout was a connection timeout, `false` or
// `undefined` otherwise.
console.log(err.connect === true);
process.exit(0);
});
Request Doc: timeout - Integer containing the number of milliseconds to wait for a server to send response headers (and start the response body) before aborting the request.
var request = require('request');
request({
url: 'myUrl',
method: 'POST',
timeout: 500,
headers: {
'Content-Type': 'application/json',
'authToken': 'myToken'
},
json: infoObj
})
.on('response', function(response) {
console.log('request response=============',response.statusCode)
})
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745659110a4638727.html
评论列表(0条)