HTTPS request with node.js?

I need to trigger a request in my node.js app. My app has a route and when it runs I am trying to hit a url that is built dynamically. So all I need is to trigger a RESt API call to somethinglike:

"https://www.domainname.com/sometext/"+ var1 +"/someothertext"

So I tried this:

var options = {
    host: 'www.domainname.com',
    port: 80,
    path: '/1.0/'+var1,
    method: 'GET'
};

//  trigger request 
request(options, function(err,response,body) {
.......
});

When I run this I get this error: options.uri is a required argument

So, my goal here is to trigger the request that hits a dynamically built url. If I had a static url I could plug in the request, it would work fine.

Infact I tried to do this:

request("https://www.domainname.com/1.0/456", function(err,response,body) {
.......
});

and this works fine.

BUT I am trying to build the url (path) dynamically with var1 and that doesn't work.

Any suggestion on how to do this?

You need a URL or an URI in the options that you pass as the first arguments to the request function

And the reason that request("https://www.domainname.com/1.0/456",function(err,response,body) { does not fail is because you are providing the url as the first argument

So change your options object to

var options = {
    url: 'https://www.domainname.com/sometext/'+ var1,
    port: 80,
    method: 'GET'
};

You can try trimming the value in var1 like var1 = var1.replace(/^\s*|\s*$/g, '');

That should remove the space.

Because the options argument that is taken by request isn't being given the correct formatted object.

The error you are receiving is because you need to send a url or uri as an option. This can be clarified here:

https://github.com/request/request

The following should do what you want:

So I tried this:

var options = {
    url: "https://www.domainname.com/sometext/"+ var1 +"/someothertext"
    method: 'GET'
};

//  trigger request 
request(options, function(err,response,body) {
  console.log(response.statusCode);
});