What is wrong with this https.request example?

56 Views Asked by At

When I try this example it never returns anything.

const https = require('https')

const options = {
  hostname: 'encrypted.google.com',
  port: 443,
  path: '/',
  method: 'GET',
  //      key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  //      cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
options.agent = new https.Agent(options);

const req = https.request(options, (res) => {
  console.log(res)
});

Question

Can anyone figure out why it doesn't work?

When I have a working example, I would like to get the cert+key working. So this the first step in that attempt.

1

There are 1 best solutions below

0
On BEST ANSWER

Your code is not exactly like the sample from the documentation, which works.

Shouldn't you call the end() function on the request object so that the request is actually completely sent?

I think that's what's missing in your snippet. Without that, the stream is not closed and the request keeps pending for more, and you never receive a response since your request is not over.