Search code examples
javascriptnode.jsget

Constructing Nodejs HTTPS get Payload


currently I am trying to use the https.get function to retrieve tokens as well as make general API calls to Yahoo. I seem to be able to get it to work when I build a long query string and add it into the url, passing the whole url string as the first argument.

var finalUrl = host + path + "?" + paramString;

return new Promise(function(resolve, reject) {
    var data = {
       "error_str": "",
       "errno": 0,
       "return_code": 0,
       "contents": ""
    }
    console.log("In promise function");
    console.log("finalUrl: " + finalUrl);
var options = {
    "host": host,
    "path": path,
    "headers": params,
    "timeout": 2
};

console.log("options: " + JSON.stringify(options));

https.get(finalUrl, (res) => {
// resolves

However, when I try https.get with the options object so I can adjust the timeout for more data intensive calls, it fails

https.get(options, (res) => {
// rejects

Printing the final url yields:

finalUrl:

https://api.login.yahoo.com/oauth/v2/get_request_token?oauth_callback=oob&oauth_consumer_key=my_consumer_key&oauth_nonce=334283&oauth_signature_method=PLAINTEXT&oauth_timestamp=1514728974491&oauth_version=1.0&xoauth_lang_pref=en-us&oauth_signature=my_comsumer_secret%26

Meanwhile, when I construct options it looks like this

{
    "host":"https://api.login.yahoo.com",
    "path":"/oauth/v2/get_request_token",
    "headers":{
        "xoauth_lang_pref":"en-us",     
        "oauth_callback": "oob",                  `                                                                                                          
        "oauth_consumer_key":"my_consumer_key",`
        "oauth_nonce":334283,
        "oauth_signature_method":"PLAINTEXT",
        "oauth_timestamp":1514728974491,
        "oauth_version":"1.0",
        "oauth_signature":"my_consumer_secret&"
    },
    "timeout":2
}

However, performing a get with this payload yields

getaddrinfo ENOTFOUND https://api.login.yahoo.com https://api.login.yahoo.com:443


Solution

  • There are two big problems with the way you're building the options object.

    The first is that the host item is supposed to specify just the destination hostname, in this case "api.login.yahoo.com". The "https:" part should be specified in a separate protocol item. This is why you're getting the ENOTFOUND error.

    The second is that the query string is supposed to be included as part of the path item. So you should build the content of the path item just as you did when you constructed finalUrl. The headers item is used for a completely different purpose. It allows you to specify additional HTTP request headers, not query string elements. You do not need a headers item here.

    You can read all about the options items in the Node.js documentation at https://nodejs.org/docs/latest-v6.x/api/http.html#http_http_request_options_callback although most of the option items are irrelevant in this case. You only need to use the host, protocol and path items. That webpage will tell you why the timeout item you have right now is ineffective.