# \[Kibana Plugin\]: Inject data (headers) from Kibana to the browser

**URL:** <https://discuss.elastic.co/t/kibana-plugin-inject-data-headers-from-kibana-to-the-browser/192283>\
**Category:** Kibana\
**Created:** [July 25, 2019, 3:59pm UTC](https://discuss.elastic.co/t/kibana-plugin-inject-data-headers-from-kibana-to-the-browser/192283 "2019-07-25T15:59:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dasko](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@Dasko](https://discuss.elastic.co/u/Dasko)\
**Post date:** [July 25, 2019, 3:59pm UTC](https://discuss.elastic.co/t/kibana-plugin-inject-data-headers-from-kibana-to-the-browser/192283/1 "2019-07-25T15:59:27Z")

</div>

Hi guys,

I'm looking for a way to make some data (headers received by the Kibana Server) available to the plugin front-end (re-use the headers to fetch some data through an external API).

It is also worth noticing that I retrieve the headers like this :

```auto
// Request interceptor, runs on every incoming request
server.ext('onRequest', async (request, h) => {
      const headers = request.headers;

      doSomethingWithHeaders(headers);

      return h.continue;
});

```

I've seen that I can use `server.injectUiAppVars` in the init method of the plugin to inject data and retrieve it in the browser.  
I'm having trouble figuring out how to retrieve the headers from the hapi requests life-cycle (via the code above) and inject them with the injectUiAppVars function. Any clues ?

Cheers

---

<div class="post-metadata">

**Author:** ![rudolf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rudolf/32/51200_2.png) [@rudolf](https://discuss.elastic.co/u/rudolf)\
**Post date:** [July 30, 2019, 7:58am UTC](https://discuss.elastic.co/t/kibana-plugin-inject-data-headers-from-kibana-to-the-browser/192283/2 "2019-07-30T07:58:56Z")

</div>

Hi Dasko,

Maybe you can give a bit more details about the problem you're trying to solve? It sounds like you are trying to provide credentials/authentication token to the frontend so that the frontend code can do a fetch request to an external API. Why are you trying read the headers of incoming requests in the hapi requests life-cycle?

If you share variables with the frontend using `server.injectUiAppVars` you can retrieve these on the browser with

```auto
import chrome from 'ui/chrome';
const myVarName = chrome.getInjected('myVarName');

```

---

<div class="post-metadata">

**Author:** ![Dasko](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@Dasko](https://discuss.elastic.co/u/Dasko)\
**Post date:** [July 30, 2019, 8:15am UTC](https://discuss.elastic.co/t/kibana-plugin-inject-data-headers-from-kibana-to-the-browser/192283/3 "2019-07-30T08:15:21Z")

</div>

@rudolf,

It is indeed what I'm trying to do. We access Kibana through a proxy, and that proxy provides two headers that should be used to grant access to an external API. To be honest, I haven't found a way to grab those headers from Kibana except using hapi life-cycle. Do you know any other way ?

Cheers,  
Dasko

---

<div class="post-metadata">

**Author:** ![rudolf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rudolf/32/51200_2.png) [@rudolf](https://discuss.elastic.co/u/rudolf)\
**Post date:** [July 30, 2019, 11:04am UTC](https://discuss.elastic.co/t/kibana-plugin-inject-data-headers-from-kibana-to-the-browser/192283/4 "2019-07-30T11:04:34Z")

</div>

So what problems are you having with using `server.ext('onRequest'`?

---

<div class="post-metadata">

**Author:** ![Dasko](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@Dasko](https://discuss.elastic.co/u/Dasko)\
**Post date:** [July 30, 2019, 12:27pm UTC](https://discuss.elastic.co/t/kibana-plugin-inject-data-headers-from-kibana-to-the-browser/192283/5 "2019-07-30T12:27:08Z")

</div>

I've been tinkering a bit with the code, and got something that seems to be working fine.  
I guess I was having trouble because I was tired of my week 😅.

I'll post what I have, if that can help anyone.

```auto
export default function (kibana) {
  return new kibana.Plugin({
    require: ['elasticsearch'],
    name: 'Archives',
    uiExports: {
      app: {
        title: 'Archives',
        description: 'Archives',
        main: 'plugins/archives/app',
        icon: 'plugins/archives/archive.svg'
      }
    },
    init: (server, options) => {
      server.ext('onRequest', async (request, h) => {
        const headers = request.headers;

        console.log(headers['x-forwarded-group'])
        server.injectUiAppVars('archives', () => {
          return {
            headers: {
              group: headers['x-forwarded-group']
            }
          }
        })

        return h.continue;
      });
    },
    config(Joi) {
      return Joi.object({
        enabled: Joi.boolean().default(true),
      }).default();
    },

  });
}

```

Thank you for your help @rudolf, I'm sorry I wasted some of your time.

---

<div class="post-metadata">

**Author:** ![rudolf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rudolf/32/51200_2.png) [@rudolf](https://discuss.elastic.co/u/rudolf)\
**Post date:** [August 1, 2019, 5:44pm UTC](https://discuss.elastic.co/t/kibana-plugin-inject-data-headers-from-kibana-to-the-browser/192283/6 "2019-08-01T17:44:25Z")

</div>

No problem, I'm glad you came right!

---

<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:** [August 29, 2019, 5:44pm UTC](https://discuss.elastic.co/t/kibana-plugin-inject-data-headers-from-kibana-to-the-browser/192283/7 "2019-08-29T17:44:28Z")

</div>

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