javascript node.js 的 HTTPS 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12137192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTPS request with node.js?
提问by zumzum
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:
我需要在我的 node.js 应用程序中触发一个请求。我的应用程序有一个路由,当它运行时,我试图访问一个动态构建的 url。所以我所需要的就是触发一个 REST API 调用,例如:
"https://www.domainname.com/sometext/"+ var1 +"/someothertext"
"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
当我运行它时,我收到此错误:
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.
因此,我的目标是触发命中动态构建的 url 的请求。如果我有一个静态 url,我可以插入请求,它会正常工作。
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.
但是我正在尝试使用 var1 动态构建 url(路径),但这是行不通的。
Any suggestion on how to do this?
关于如何做到这一点的任何建议?
回答by Clyde Lobo
You need a URL or an URI in the options that you pass as the first arguments to the request
function
您需要在作为第一个参数传递给request
函数的选项中使用 URL 或 URI
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
request("https://www.domainname.com/1.0/456",function(err,response,body) {
没有失败的原因是因为您提供了 url 作为第一个参数
So change your options
object to
所以改变你的options
对象
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, '')
;
您可以尝试在var1
like 中
修剪值var1 = var1.replace(/^\s*|\s*$/g, '')
;
That should remove the space.
那应该删除空间。
回答by Menztrual
Because the options
argument that is taken by request isn't being given the correct formatted object.
因为options
请求采用的参数没有得到正确格式化的对象。
The error you are receiving is because you need to send a url or uri
as an option. This can be clarified here:
您收到的错误是因为您需要发送一个url or uri
作为选项。这可以在这里澄清:
https://github.com/request/request
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);
});