# CORS: GET Request from plugin

**URL:** <https://discuss.elastic.co/t/cors-get-request-from-plugin/123742>\
**Category:** Kibana\
**Created:** [March 13, 2018, 1:46pm UTC](https://discuss.elastic.co/t/cors-get-request-from-plugin/123742 "2018-03-13T13:46:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjh](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@pjh](https://discuss.elastic.co/u/pjh)\
**Post date:** [March 13, 2018, 1:46pm UTC](https://discuss.elastic.co/t/cors-get-request-from-plugin/123742/1 "2018-03-13T13:46:34Z")

</div>

Hey,

I am trying to make a very simple get request from a plugin I'm developing. I have tried a couple of things. Attached is an excerpt of some of this code.

```
const request = require('request');

function getRandom() {
  return Math.random() * 10;
}

function getInfo() {
  let info;

  console.log("magic happens here")

  request('http://google.com', function (err, res, body) {
    info = body.toString();
  });

  return info;
}

export default function (server) {

  server.route({
    path: '/yoinfo',
    method: 'GET',
    handler(req, reply) {
      reply({ info: getInfo(), name: "jimbo", rand: getRandom() });
    }
  });
}

```

Both name and rand are returned in the payload, but info is not. Any idea how I can make a simple http get request within the plugin server directory?

Also, how to log to the kibana stdout?

Thanks

---

<div class="post-metadata">

**Author:** ![pjh](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@pjh](https://discuss.elastic.co/u/pjh)\
**Post date:** [March 13, 2018, 9:37pm UTC](https://discuss.elastic.co/t/cors-get-request-from-plugin/123742/2 "2018-03-13T21:37:25Z")

</div>

Ok, so it turns out that it was a CORS issue (I assume from the output from:

```
var http = require('http');

export default function (server) {

  const url = `${server.info.uri}/api/status`;

  var metrics;

  http.get(url, function(res){
    var body = '';

    res.on('data', function(chunk){
        body += chunk;
    });

    res.on('end', function(){
        metrics = JSON.parse(body);
    });
  }).on('error', function(e){
        metrics = "Got an error: " + e;
  });

  server.route({
    path: '/metrics',
    method: 'GET',
    handler(req, reply) {
        reply({
            metrics: metrics,
            other: "jimbo"
        });
    }
  });
}

```

Output

```
{
metrics: "Got an error: Error: connect ECONNREFUSED 127.0.0.1:5601",
other: "jimbo"
}

```

If you change the kibana url to localhost:9200 it will work. How can I overcome this?

---

<div class="post-metadata">

**Author:** ![spalger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spalger/32/14092_2.png) [@spalger](https://discuss.elastic.co/u/spalger)\
**Post date:** [March 14, 2018, 3:55am UTC](https://discuss.elastic.co/t/cors-get-request-from-plugin/123742/3 "2018-03-14T03:55:25Z")

</div>

In the first example your call to `getInfo()` needs to wait for `request()` to call the callback before returning. The callback is called asynchronously, so you'll need to use a promise or another callback to get this working.

```auto
const request = require('request');

function getRandom() {
  return Math.random() * 10;
}

function getInfo(callback) {
  console.log("magic happens here")

  request('http://google.com', function (error, res, body) {
    if (error) {
      // always check for errors
      callback(error);

      // return to prevent the rest of this function from running
      return;
    }

    // pass the body to the callback without an error
    callback(null, body.toString());
  });
}

export default function (server) {

  server.route({
    path: '/yoinfo',
    method: 'GET',
    handler(req, reply) {
      // call getInfo and it will call getInfoCallback() when it is done 
      getInfo(function getInfoCallback(error, info) {
        if (error) {
          // always check for errors
          reply(error);
          return
        }

        reply({
          info: info,
          name: "jimbo",
          rand: getRandom()
        });
      });
    }
  });
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 11, 2018, 3:55am UTC](https://discuss.elastic.co/t/cors-get-request-from-plugin/123742/4 "2018-04-11T03:55:28Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
