最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTTPS request with node.js? - Stack Overflow

programmeradmin7浏览0评论

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:

"/"+ var1 +"/someothertext"

So I tried this:

var options = {
    host: 'www.domainname',
    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(".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?

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./sometext/"+ var1 +"/someothertext"

So I tried this:

var options = {
    host: 'www.domainname.',
    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./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?

Share Improve this question edited Aug 27, 2012 at 6:43 Menztrual 41.6k12 gold badges59 silver badges70 bronze badges asked Aug 27, 2012 at 6:33 zumzumzumzum 20.4k31 gold badges129 silver badges208 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

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./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./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./request/request

The following should do what you want:

So I tried this:

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

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

评论列表(0)

  1. 暂无评论