2

I'm trying to send the same http request multiple times. I just put the request in a loop, but when I run the code it shows the response 1 time.

const req = https.request(options, res => {
  console.log(`statusCode: ${res.statusCode} ${res.statusMessage}`)

  res.on('data', d => {
    process.stdout.write(d)
  })
})

for(i=0; i<3; i++){ 
      req.write(data)
}
6
  • 1
    What are you trying to do? What are you trying to use this with? Commented Nov 1, 2021 at 13:48
  • 1
    @evolutionxbox I am trying to send a post request to an external api. It works well, but I get just one response. I am using basic node.js Commented Nov 1, 2021 at 13:54
  • 1
    I think you need to keep the connection open Commented Nov 1, 2021 at 13:58
  • But req.write does not make a HTTP request...? Commented Nov 1, 2021 at 14:06
  • @jeremyThile I know. I have configured the request before Commented Nov 1, 2021 at 14:10

1 Answer 1

1

You should put the request inside the for loop:

for(i=0; i < 3; i++){ 
const req = https.request(options, res => {
  console.log(`statusCode: ${res.statusCode} ${res.statusMessage}`)

  res.on('data', d => {
    process.stdout.write(d)
  })
});
req.write(data);
}
Sign up to request clarification or add additional context in comments.

Comments

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.