You are on page 1of 6

3/15/2018 Node JS: Making https request via proxy

CodingMiles.com HOME

22 MARCH 2015 / GET

Node JS: Making https


request via proxy
Struggled quite a bit before getting this worked. Had a
requirement where I had to make a GET/POST call
from node JS code to external webservice API over
tunnel proxy. Posting multiple solutions that worked
for me:

Using https-proxy-agent module
and request module
Install the following modules:

npm install -g request https-proxy-agent

In case you are connecting to an http:// endpoint, you could install:

npm install -g http-proxy-agent

Then:

http://codingmiles.com/node-js-making-https-request-via-proxy/ 1/6
3/15/2018 Node JS: Making https request via proxy

var HttpsProxyAgent = require('https-proxy-agent');


var request = require('request');
var proxy = 'http://127.0.0.1:3128';
var agent = new HttpsProxyAgent(proxy);
request({
uri: "https://example.com/api",
method: "POST",
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
agent: agent,
timeout: 10000,
followRedirect: true,
maxRedirects: 10,
body: "name=john"
}, function(error, response, body) {
console.log("Error" + error);
console.log("Response: " + response);
console.log("Body: " + body);
});

Using https-proxy-agent module
and https module

var https = require('https');


var HttpsProxyAgent = require('https-proxy-agent');
var proxy = 'http://127.0.0.1:3128';
var agent = new HttpsProxyAgent(proxy);
var post_req = https.request({
host: 'example.com',
port: '443',
path: '/api',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'

http://codingmiles.com/node-js-making-https-request-via-proxy/ 2/6
3/15/2018 Node JS: Making https request via proxy

},
agent: agent,
timeout: 10000,
followRedirect: true,
maxRedirects: 10
}, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log('Response: ' + chunk);
});
});
post_req.write("name=john");
post_req.end();

References:
https://nodejs.org/api/https.html

http://blog.vanamco.com/proxy-requests-in-node-js/ (Custom
implementation of HttpsProxyAgent)

https://www.npmjs.com/package/https-proxy-agent

Ankur Agarwal Read More


Read more posts by this author.

12 Comments CodingMiles 
1 Login

Sort by Best
 Recommend 2 ⤤ Share

Join the discussion…

LOG IN WITH
http://codingmiles.com/node-js-making-https-request-via-proxy/ 3/6
3/15/2018 Node JS: Making https request via proxy
LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

Marc Busch • a year ago


Good grief, You're examples were the ONLY ones that worked after research & trial & errors
for several hours - THANKS!
1△ ▽ • Reply • Share ›

Ankur Agarwal Mod > Marc Busch • a year ago


Glad it helped! Cheers :-)
△ ▽ • Reply • Share ›

Mike • 4 months ago


I found that your code can't reuse the connection after each HTTPS request. It makes a new
connection every time when I do a HTTPS request. Do you have any way to fix it? Thanks
△ ▽ • Reply • Share ›

niladri roy • 5 months ago


im getting the following error:
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error:
Error: write EPROTO 101057795:error:1407
70FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown
protocol:openssl\ssl\s23_clnt.c:794:
△ ▽ • Reply • Share ›

Nithin Satheesan • 8 months ago


Thanks alot for the help. I researched a lot. Nothing worked except this one. Keep up the
good work!
△ ▽ • Reply • Share ›

Alone Warrior • 2 years ago


How do i send the username/password for the proxy?
△ ▽ • Reply • Share ›

Ankur Agarwal Mod > Alone Warrior • 2 years ago


In above example, for passing username / password, you can change your proxy url
to something like:

var proxy = 'http://" + username + ":" + password + "@127.0.0.1:3128';


△ ▽ • Reply • Share ›

Onofre Martins • 2 years ago


perfect. i can turn off proxy config when I'm at home, and turn on on my work
△ ▽ • Reply • Share ›
http://codingmiles.com/node-js-making-https-request-via-proxy/ 4/6
3/15/2018 Node JS: Making https request via proxy

Ankur Agarwal Mod > Onofre Martins • 2 years ago


Glad it helped!
△ ▽ • Reply • Share ›

Rob Caldecott • 3 years ago


Very useful. Interestingly, if the HTTP_PROXY env var is set on Windows, then 'request' will
ignore the https-proxy-agent, which confused me for a while.
△ ▽ • Reply • Share ›

Ankur Agarwal Mod > Rob Caldecott • 3 years ago

Interesting! Need to check on this to see what's happening exactly under the hood.
Thanks for the insight Rob.
△ ▽ • Reply • Share ›

David Kassa • 3 years ago


thanks. This helped.
△ ▽ • Reply • Share ›

ALSO ON CODINGMILES

Sorting Algorithms: Selection Sort using Adding custom fonts to react native for
JavaScript Android
5 comments • 3 years ago 2 comments • 2 years ago
Jay — Appreciate it. Thanks! :) Ankur Agarwal — Did you type the correct font
name at fontFamily: 'NewWalt'? Note that
'NewWalt' is not the file name. It is the name

NodeJS – Bulk update to MongoDB using Sorting Algorithms: Insertion Sort using

CONFIG ALGORITHMS

Step by step Mongo DB Sorting Algorithms:


sharded cluster deployment
http://codingmiles.com/node-js-making-https-request-via-proxy/
Insertion Sort using 5/6
3/15/2018 Node JS: Making https request via proxy
sharded cluster deployment Insertion Sort using
Mongo db shard cluster deployment JavaScript
This tutorial explains the step by step This post covers the essentials of
process to deploy a sharded MongoDB insertion sort using JavaScript. We will
cluster. Mongo DB version used is 3.2. use a simple array to demonstrate the
This tutorial also assumes OS to be concepts of Insertion Sort before getting
into code. Concept: In insertion sort, we
divide
ANKUR AGARWAL
ANKUR AGARWAL

CodingMiles.com © 2018
Latest Posts Ghost

http://codingmiles.com/node-js-making-https-request-via-proxy/ 6/6

You might also like