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.com",
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.com"
It is working.
Why?
http.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 components of http url scheme. You can check this article (link) that explains very clearly how those components 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.