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

javascript - What is correct format of Node.js http.request options hostname parameter? - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 4
  1. 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.

  2. 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.

发布评论

评论列表(0)

  1. 暂无评论