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

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:

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.

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

Thank you
Bhavya

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:

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
                }
                });
        }
        )
}

Hi there Disha,

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

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 });
  }
);
3 Likes

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