vedant sali
vedant sali

Reputation: 80

node.js request module giving error for http call [Error: Invalid protocol: null]

I am using request module for node.js to make HTTP GET call to an api having address like: http://x.x.xxx.xx:8000/names but the call is returning an error [Error: Invalid protocol: null].

My api call looks like this:

request.get({
    uri: 'http://x.x.xxx.xx:8000/names'
}, function(err, res, body){
    if(err == null){
        res.status(200).send(data);
    }else{
        console.log(err);
    }});

Note: I have used proxies for npm through "npm config set proxy" and "npm config set https proxy" . Removing them is also not solving the problem.

Thanks

Upvotes: 2

Views: 16445

Answers (4)

Richard Tyler Miles
Richard Tyler Miles

Reputation: 685

If you are connecting to a PHP development server please see this thread for solution

Upvotes: 0

user2258468
user2258468

Reputation: 41

I had my HTTP_PROXY environment variable incorrectly set

Upvotes: 0

Marcos Casagrande
Marcos Casagrande

Reputation: 40374

Since this, works perfectly fine, I'll say that the problem is in your proxy config.

request.get({
    uri: 'http://localhost:8000/names'
}, function(err, res, body){

});

Try this:

npm config set proxy http://usr:pwd@host:port

Upvotes: 4

Hamel Kothari
Hamel Kothari

Reputation: 737

Using the request.get, you must first specify the url as a string, followed by a dictionary with options, if you want to pass an option. The error is likely due to confusion that you're passing in a dictionary. Either try using request() with a dictionary parameter or request.get('http://xx.xx.xx.xx:8080/')

Docs for reference: https://www.npmjs.com/package/request#convenience-methods

Upvotes: 1

Related Questions