admin管理员组文章数量:1026989
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)
})
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)
})
本文标签: javascriptWhere set timeout with post request using request package in nodejsStack Overflow
版权声明:本文标题:javascript - Where set timeout with post request using request package in nodejs - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745659108a2161781.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论