0

I have implemented a GET request via request module in Nodejs in an application. I need to display the response over html page. However, the response returned is undefined. Also, error and body message is also undefined.

I tried running it seperately with "node file.js", it works fine. Do I have to add anything else while using it in application. I also tried htttp, curl-request and request-promise modules, all of them return undefined.

    var request  = require(['request']);
    var headers = {
            'Accept': 'text/json',
            'X-Auth-Token': tk,
            'User-Agent': 'request'
    };
    var options = {
            url: 'https://api.github.com/repos/request/request',
            headers: headers,
            method: 'GET',
            json: true
    };
    function callback(error, response, body){

            console.log(response);
            console.log(error);
          if (!error && response.statusCode == 200) {
                    console.log(body);
                    return body;
          }
    }

 request(options, callback);
tbking
  • 8,796
  • 2
  • 20
  • 33
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – nicholaswmin Jan 29 '19 at 05:18
  • 1
    Why are you importing with `require(['request'])` and not `require('request')`? Also, the correct json content type is `application/json` and not `text/json`. – tbking Jan 29 '19 at 05:21
  • When i use require('request'), it throws error as "Module request cannot be loaded into context." – Kushagra Srivastava Feb 01 '19 at 04:17

1 Answers1

-1

I just had the same issue and found out that the server certificate was not fully trusted. As a TEMPORARY solution you could try to add strictSSL: false to your options object.

If it works like this (and assuming that you did not really send the request to "https://api.github.com/repos/request/request") contact the service provider and tell them about the certificate issues.

schlicki
  • 374
  • 4
  • 14