Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTPS request failed with proxy #5

Closed
larvata opened this issue Apr 6, 2020 · 2 comments
Closed

HTTPS request failed with proxy #5

larvata opened this issue Apr 6, 2020 · 2 comments

Comments

@larvata
Copy link

larvata commented Apr 6, 2020

I'd like to use the following code snippet to fetch the page, but an empty string was responded.

const { client } = require('request-compose');

client({
  url: 'https://google.com',
  proxy: process.env.https_proxy,
}).then(({ res, body }) => {
  // body is ''
  console.log('body:', body);
}).catch((err) => {
  console.log(err);
});
$ export https_proxy=http://127.0.0.1:8888

$ curl https://google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>


$ node .
body:
@larvata larvata changed the title proxy setting not works HTTPS request failed with proxy Apr 6, 2020
@simov
Copy link
Owner

simov commented Apr 6, 2020

For HTTPS you have to use a tunnel agent:

var tunnel = require('tunnel')
var agent = tunnel.httpsOverHttp({
  proxy: {
    host: 'localhost',
    port: 8888
  }
})

Then in your script you pass the agent instead:

client({
  url: 'https://google.com',
  agent,
})

Also you might want to suppress certificate errors:

NODE_TLS_REJECT_UNAUTHORIZED=0 node script.js

@larvata
Copy link
Author

larvata commented Apr 6, 2020

it works, thank you.

@larvata larvata closed this as completed Apr 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants