Kong custom Plugin using Javascript PDK to make REST call

HI,

I am trying to create a kong plugin which can make rest call. Following is my implementation.

restcall.js

const axios = require('axios');

class RestCallPlugin {
    async access(kong) {
        this.kong = kong;
        kong.log.err("testing logging");
        let apiurl = `https://gorest.co.in/public/v1/users`;
        kong.log.err("testing api call");
        axios({
            method: 'get',
            url: apiurl
        }).then(function (response) {
            kong.log.err("response", response);
        }, (error) => {
            kong.log.err("error", error);
        });
    }
}
module.exports = {
    Plugin: RestCallPlugin,
    Version: "0.1.0"
};

Dockerfile

FROM kong:latest

# Install the js-pluginserver
USER root
RUN apk add --update nodejs npm python2 make g++
RUN npm install --unsafe -g kong-pdk@0.3.0
ENV term xterm
RUN apk add --update vim nano

docker-compose.yml

version: '3.7'
volumes:
  kong_data: {}
networks:
  kong-net: null
services:
  kong-database:
    image: 'postgres:9.6'
    container_name: kong-postgres
    restart: on-failure
    networks:
      - kong-net
    volumes:
      - 'kong_data:/var/lib/postgresql/data'
    environment:
      POSTGRES_USER: kong
      POSTGRES_PASSWORD: kong
      POSTGRES_DB: kong
    ports:
      - '5432:5432'
    healthcheck:
      test:
        - CMD
        - pg_isready
        - '-U'
        - kong
      interval: 30s
      timeout: 30s
      retries: 3
  kong-migration:
    image: 'kong:latest'
    command: kong migrations bootstrap
    networks:
      - kong-net
    restart: on-failure
    environment:
      KONG_DATABASE: postgres
      KONG_PG_HOST: kong-database
      KONG_PG_DATABASE: kong
      KONG_PG_USER: kong
      KONG_PG_PASSWORD: kong
    depends_on:
      - kong-database
  kong:
    image: 'kong:latest'
    restart: on-failure
    networks:
      - kong-net
    volumes:
      - 'D:\Research\Kong\plugins:/usr/local/kong/js-plugins'
      - 'D:\Research\Kong\config:/usr/local/kong/declarative'
      - D:\Research\Kong\certs:/ssl
    environment:
      KONG_DATABASE: postgres
      KONG_PG_HOST: kong-database
      KONG_PG_DATABASE: kong
      KONG_PG_USER: kong
      KONG_PG_PASSWORD: kong
      KONG_SSL_CERT: /ssl/server.crt
      KONG_SSL_CERT_KEY: /ssl/server.key
      # Enable the JS plugin server
      KONG_PLUGINSERVER_NAMES: js
      KONG_PLUGINSERVER_JS_SOCKET: /usr/local/kong/js_pluginserver.sock
      KONG_PLUGINSERVER_JS_START_CMD: /usr/local/bin/kong-js-pluginserver -v --plugins-directory /usr/local/kong/js-plugins
      KONG_PLUGINSERVER_JS_QUERY_CMD: /usr/local/bin/kong-js-pluginserver --plugins-directory /usr/local/kong/js-plugins --dump-all-plugins
      # Allow plugins to be used. The plugin name is your JS file name e.g. hello.js
      KONG_PLUGINS: bundled, azureauthbt, restcall
      KONG_PROXY_LISTEN: '0.0.0.0:8000'
      KONG_PROXY_LISTEN_SSL: '0.0.0.0:8443'
      KONG_ADMIN_LISTEN: '0.0.0.0:8001'
      KONG_ADMIN_LISTEN_SSL: '0.0.0.0:8444'
    depends_on:
      - kong-database
    healthcheck:
      test:
        - CMD
        - kong
        - health
      interval: 10s
      timeout: 10s
      retries: 10
    ports:
      - '61001:8000'
      - '61002:8001'
      - '61003:8443'
      - '61004:8444'
  kong-ui:
    image: 'pocketdigi/kong-admin-ui:0.5.3'
    restart: on-failure
    networks:
      - kong-net
    ports:
      - '8899:80'

I am able to get the first two log messages. I am mentioning it belo.

kong-kong-1 | 2022/01/26 02:33:58 [error] 1105#0: *308 [kong] mp_rpc.lua:286 [restcall] testing logging, client: 172.20.0.1, server: kong, request: “GET /portal/api/users HTTP/1.1”, host: “localhost:61001”
kong-kong-1 | 2022/01/26 02:33:58 [error] 1105#0: *308 [kong] mp_rpc.lua:286 [restcall] testing api call, client: 172.20.0.1, server: kong, request: “GET /portal/api/users HTTP/1.1”, host: “localhost:61001”
kong-kong-1 | 172.20.0.1 - - [26/Jan/2022:02:33:58 +0000] “GET /portal/api/users HTTP/1.1” 304 0 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36 Edg/97.0.1072.69”

But I am not able to get either the success response not the failure message from the rest call. Is there any way to debug is the API call happening. Or in kong pdk is there any specific way to make rest call and handle it.

Thanks

HI.

Last day I came across a npm plugin called ‘unirest’ https://github.com/Kong/unirest-nodejs for supporting rest call from kong gateway. I updated the code as below.

const unirest = require('unirest');

class RestCallPlugin {
    async access(kong) {
        this.kong = kong;
        let apiurl = `https://gorest.co.in/public/v1/users`;
        kong.log("Testing API Call");
        var Request = unirest.get(apiurl);
        Request.end(function (response) {
            console.log(response.body)
        });
    }
}
module.exports = {
    Plugin: RestCallPlugin,
    Version: "0.1.0"
};

But still I am not able to send the request and get the response in the custom plugin. I am not finding a proper documentation or any thread to solve this issue.

Thanks