arkoak
arkoak

Reputation: 2497

Read data from a url behind a proxy

WEB Server I want to connect to

I have a web service running in a private network. This server is a web service which I can see working in the browser if I set the socks proxy in the browser.

My Service

I need node.js server on my machine to use the socks proxy to connect and call the web server

My UseCase

I need to do post requests for xml data as well as do some get requests.

My Problem

My app is not able to connect to the server hidden behind the socks proxy. I do not want to set the global proxy for node or anything, only for one part of the app.

Updated : Working Solution

While the answer directs in the correct direction, I will include the final working solution here for reference as it needed a few modifications to the examples on github

    var shttp = require('socks5-http-client');
    var options = {} ; 
    options.host = 'ip.of.web.service';
    options.port = 1919;     //port of webservice
    options.path = '/control/getjson';    //path on webservice to get
    options.socksPort = 8778;       //socks proxy port
    options.socksHost = 'ip.of.socks.proxy';  

    var req = shttp.get(options, function (res) {
        res.setEncoding('utf8');
        res.on('readable', function () { 
            callback(res);   //send response to my function for further processing.
        });
    });

Upvotes: 0

Views: 202

Answers (1)

Prabhu Velayutham
Prabhu Velayutham

Reputation: 411

Using socks proxy is not natively supported in the built in http client object.

Following 2 libraries makes it easy to connect to http endpoints through a socks proxy. Give it a try

Use socks5-http-client for connecting to http endpoints Use socks5-https-client for connecting to https endpoints

Upvotes: 2

Related Questions