# Redirect post call from kibana backend to another server using Hapi/h2o2 proxy handler in kibana plugin development

**URL:** <https://discuss.elastic.co/t/redirect-post-call-from-kibana-backend-to-another-server-using-hapi-h2o2-proxy-handler-in-kibana-plugin-development/278066>\
**Category:** Kibana\
**Tags:** kibana-plugin-development\
**Created:** [July 7, 2021, 12:12pm UTC](https://discuss.elastic.co/t/redirect-post-call-from-kibana-backend-to-another-server-using-hapi-h2o2-proxy-handler-in-kibana-plugin-development/278066 "2021-07-07T12:12:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Disha\_Bodade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/disha_bodade/32/84522_2.png) [@Disha\_Bodade](https://discuss.elastic.co/u/Disha_Bodade)\
**Post date:** [July 7, 2021, 12:12pm UTC](https://discuss.elastic.co/t/redirect-post-call-from-kibana-backend-to-another-server-using-hapi-h2o2-proxy-handler-in-kibana-plugin-development/278066/1 "2021-07-07T12:12:20Z")

</div>

I have created a kibana plugin using the following procedure:

1. goto kibana source directory
2. node scripts/generate\_plugin test\_plugin
3. build plugin with #yarn plugin-helpers build

I modified UI as it has some forms in frontend to accept user inputs. I created a routes in routes/index.ts has post call to handle this requests.

The post calls from kibana I want to redirect it to node server on port 3000.  
Kibana plugin has @Hapi/h2o2 module installed which can be use for proxy handling, but I am not getting how to use it.

kibana source has interface “BasePathProxyServerOptions” for proxy handler in path  
kibana/src/core/server/http//base\_path\_proxy\_server.ts

I am new to nodejs, I am not sure how I will able to use this interface in my routes.  
my post call in file /kibana/plugins/test\_plugin/server/routes/index.ts:

```auto
import { IRouter } from ‘…/…/…/…/src/core/server’;
import { schema } from ‘@kbn/config-schema’;
import * as hapi from ‘@hapi/h2o2’

export function defineRoutes(router: IRouter) {
router.post(
{
path: ‘/api/test_plugin/setIdentifier’,
handler:
{
proxy:
{
host: ‘node_server’,
port: ‘3000’,
protocol: ‘http’,
uri: ‘http://node_server:3000/v1.0/setIdentifier’,
}
},
validate:
{
body: schema.object({
name: schema.string(),
}),
},
},
async(context, request, response) => {
return response.ok({
body: {
message: `Received Identifier:${request.body.name}`
}
});
}
)

```

can anybody help to use proxy handler in routes. My call is not getting redirected with the above code I have written.

---

<div class="post-metadata">

**Author:** ![bhavyarm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bhavyarm/32/22392_2.png) [@bhavyarm](https://discuss.elastic.co/u/bhavyarm)\
**Post date:** [July 7, 2021, 9:55pm UTC](https://discuss.elastic.co/t/redirect-post-call-from-kibana-backend-to-another-server-using-hapi-h2o2-proxy-handler-in-kibana-plugin-development/278066/2 "2021-07-07T21:55:52Z")

</div>

@tiagocosta/ @jbudz can I please get some eyes on this?

Thank you  
Bhavya

---

<div class="post-metadata">

**Author:** ![Disha\_Bodade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/disha_bodade/32/84522_2.png) [@Disha\_Bodade](https://discuss.elastic.co/u/Disha_Bodade)\
**Post date:** [July 9, 2021, 5:19am UTC](https://discuss.elastic.co/t/redirect-post-call-from-kibana-backend-to-another-server-using-hapi-h2o2-proxy-handler-in-kibana-plugin-development/278066/3 "2021-07-09T05:19:22Z")

</div>

I have tried to use http.request to send a post request to that external REST server, but it also not working as expected. when i added a CURL command with child\_process.exec it's working fine. how can i replace that curl command with some http, or axios, or using Hapi itself?  
Below is my code:

```auto
import { IRouter } from '../../../../src/core/server';
import { schema } from '@kbn/config-schema';
import * as command from 'child_process';

export function defineRoutes(router: IRouter) {

function runCmd(cmd)
{
  var resp = command.execSync(cmd);
  var result = resp.toString('UTF8');
  return result;
}

router.post(
{
        path: '/api/test_plugin/setIdentifier',
        validate:
        {
                body: schema.object({
                        name: schema.string(),
                }),
                },
},
        async(context, request, response) => {
        var apiExec = `curl -s -d '{"name": "${request.body.name}" }' -H 'Content-Type: application/json' -H 'accept: application/json' http://node_server:3000/v1.0/setIdentifier`;
        console.log(apiExec);
        var result = JSON.parse(runCmd(apiExec));
        return response.ok({
        body:{
                error: result.name,
                statusCode: result.errorCode,
                message: result.msg
                }
                });
        }
        )
}

```

---

<div class="post-metadata">

**Author:** ![joshdover](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joshdover/32/42020_2.png) [@joshdover](https://discuss.elastic.co/u/joshdover)\
**Post date:** [July 28, 2021, 3:26pm UTC](https://discuss.elastic.co/t/redirect-post-call-from-kibana-backend-to-another-server-using-hapi-h2o2-proxy-handler-in-kibana-plugin-development/278066/4 "2021-07-28T15:26:03Z")

</div>

Hi there Disha,

I'd recommend using `node-fetch` which is already packaged with Kibana like this:

```auto
import fetch from 'node-fetch';

router.post(
  { ... },
  async (context, req, res) => {
    const response = await fetch('http://node_server:3000/v1.0/setIdentifier', {
      body: JSON.stringify({ name: req.body.name }),
      headers: {
        content-type: 'application/json'
      }
    });
    const result = await response.json();
    
    return response.ok({ body: { error: result.name, statusCode: result.errorCode, message: result.msg });
  }
);
```

---

<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 25, 2021, 3:27pm UTC](https://discuss.elastic.co/t/redirect-post-call-from-kibana-backend-to-another-server-using-hapi-h2o2-proxy-handler-in-kibana-plugin-development/278066/5 "2021-08-25T15:27:02Z")

</div>

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