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
requestfunctionAnd 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
optionsobject toYou can try trimming the value in
var1likevar1 = var1.replace(/^\s*|\s*$/g, '');That should remove the space.