0

I am trying to set the timeout values inside request module of node.js but as per the given time its not returning the response. I am explaining my code below.

postRequestOptions.url = `${nsoObj.protocol}://${nsoObj.ipAddress}:${nsoObj.httpPortNbr}/jsonrpc`;
    postRequestOptions.headers = {
      'Content-Type': 'application/json'
    };
    postRequestOptions.body = {
      jsonrpc: '2.0',
      id: 1,
      method: 'login',
      timeout: 5000,
      params: {
        user: nsoObj.userName,
        passwd: nsoObj.password
      }
    };

    request(postRequestOptions, (error, response, loginResponse) => {
      console.log('\n error: ', error);
      console.log('\n loginResponse: ', loginResponse);

      if (error || !loginResponse.id) {
        responseObj = {
          status : 'error',
          msg : `Error occurred while performing Login into "${nsoObj.nsoNickName}"  Instance. ${error}`,
          body : null
        };
        reject(responseObj);
      } else {
        loginResponse.sessionId = response.headers['set-cookie'][0];
        responseObj = {
          status : 'success',
          msg : `Successfully performing Login into "${nsoObj.nsoNickName}"  Instance`,
          body : loginResponse
        };
        resolve(responseObj);
      }
    });
  });

Here I am using request module of node.js and set timeout of 5ms. But When I am running this its taking 2min to sent back the timeout error response.

Here I need if this request could not sent back the response within 5 milisecond then it should return the timeout error.

1 Answer 1

1

delete timeout from body and using postRequestOptions.timeout = 5000; so do like this:

postRequestOptions.url = `${nsoObj.protocol}://${nsoObj.ipAddress}:${nsoObj.httpPortNbr}/jsonrpc`;
    postRequestOptions.headers = {
      'Content-Type': 'application/json'
    };
    postRequestOptions.body = {
      jsonrpc: '2.0',
      id: 1,
      method: 'login',
      params: {
        user: nsoObj.userName,
        passwd: nsoObj.password
      }
    };
    postRequestOptions.timeout = 5000;//should be outside the body
    request(postRequestOptions, (error, response, loginResponse) => {
      console.log('\n error: ', error);
      console.log('\n loginResponse: ', loginResponse);

      if (error || !loginResponse.id) {
        responseObj = {
          status : 'error',
          msg : `Error occurred while performing Login into "${nsoObj.nsoNickName}"  Instance. ${error}`,
          body : null
        };
        reject(responseObj);
      } else {
        loginResponse.sessionId = response.headers['set-cookie'][0];
        responseObj = {
          status : 'success',
          msg : `Successfully performing Login into "${nsoObj.nsoNickName}"  Instance`,
          body : loginResponse
        };
        resolve(responseObj);
      }
    });
  });
Sign up to request clarification or add additional context in comments.

9 Comments

I just added as per you but same issue. Its taking around 2min.
I think, your request is completely in correct, why the method is login?
No, its giving right result in positive test case. This is json rpc call.
can you send me the link of module in npm?
its the same request module in npm. now I am trying connect-timeout module.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.