最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Where set timeout with post request using request package in nodejs - Stack Overflow

matteradmin8PV0评论

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

{ 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 }

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
  • 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
 |  Show 3 more ments

2 Answers 2

Reset to default 1

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

      })
Post a comment

comment list (0)

  1. No comments so far