admin管理员组文章数量:1335887
I am trying to make a request to an Instagram API, I have troubles to understand why http.request hostname parameter from options.
Yet here is my code:
const http = require("http");
const https = require("https");
function requestInstagramData(){
var options = {
protocol: "https:",
hostname: "",
path: "/v1/tags/fashion?access_token=3681332213.81b69f2.88020902f003411196c3f4423912f547",
method: "GET"
};
var instaRequest = https.request(options);
instaRequest.on("response", function(res){
res.on("data", function(data){
console.log("data has arrived");
});
console.log("response");
console.log(res.statusCode);
console.log(res.statusMessage);
});
instaRequest.end();
}
requestInstagramData();
This code doesnt work, but if I change hostname in options object to
hostname: "api.instagram"
It is working.
Why?
I am trying to make a request to an Instagram API, I have troubles to understand why http.request hostname parameter from options.
Yet here is my code:
const http = require("http");
const https = require("https");
function requestInstagramData(){
var options = {
protocol: "https:",
hostname: "https://api.instagram.",
path: "/v1/tags/fashion?access_token=3681332213.81b69f2.88020902f003411196c3f4423912f547",
method: "GET"
};
var instaRequest = https.request(options);
instaRequest.on("response", function(res){
res.on("data", function(data){
console.log("data has arrived");
});
console.log("response");
console.log(res.statusCode);
console.log(res.statusMessage);
});
instaRequest.end();
}
requestInstagramData();
This code doesnt work, but if I change hostname in options object to
hostname: "api.instagram."
It is working.
Why?
Share Improve this question asked Mar 19, 2017 at 16:56 margaritamargarita 8942 gold badges10 silver badges21 bronze badges 4- en.wikipedia/wiki/Hostname – SLaks Commented Mar 19, 2017 at 16:58
- 1 cause that's the hostname – parwatcodes Commented Mar 19, 2017 at 17:00
- The reason why I am asking is because I have seen numerous examples on the web that include http:// or https:// in hostname parameter. This is why I had the confusion. – margarita Commented Mar 19, 2017 at 17:27
- I hope my answer helps. I noticed you got a couple of downvotes. I don't see this as a bad question. But if you like to improve the way you ask your questions on SO, please read this: stackoverflow./help/how-to-ask – Nick Commented Mar 26, 2017 at 17:28
2 Answers
Reset to default 4http.request is a function like any other function. To use it properly you need to know what input it can accept, for this we usually check the documentation.
Documentation of http.request (link) says the input, the argument, must be an object consisting of protocol, hostname, path and etc. These are all the standard ponents of http url scheme. You can check this article (link) that explains very clearly how those ponents are named.
Keeping those two in mind. It doesn't work with "http://" as that is the protocol and shouldn't be included in hostname.
As you already found out when you tried "api.instagram.", the hostname property is for specifying the host only, and host names by definition do not include a protocol. The protocol goes into the protocol property, just the way you have already done.
本文标签: javascriptWhat is correct format of Nodejs httprequest options hostname parameterStack Overflow
版权声明:本文标题:javascript - What is correct format of Node.js http.request options hostname parameter? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742396431a2467014.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论