request api on localhost from local openwhisk action

383 views Asked by At

I've setup a local instance of openwhisk running on docker using openwhisk-devtools. I've created an action that attempts to hit an API on my machine running at port 8081. The code is something likes this:

import axios from 'axios';

async function main () {

  let response = null;

  try {
    response = await axios.get('http://localhost:8081/api/health-check');
  } catch (error) {
    return {
      payload: {
        error: error
      }
    }
  }

  return {
    payload: {
      headers: response.headers
    }
  };
}

global.main = main;

The error I get is:

{
  "code": "ECONNREFUSED",
  "config": {
      "headers": {
          "Accept": "application/json, text/plain, */*",
          "User-Agent": "axios/0.20.0"
      },
      "maxBodyLength": -1,
      "maxContentLength": -1,
      "method": "get",
      "timeout": 0,
      "transformRequest": [
          null
      ],
      "transformResponse": [
          null
      ],
      "url": "http://localhost:8081/api/health-check",
      "xsrfCookieName": "XSRF-TOKEN",
      "xsrfHeaderName": "X-XSRF-TOKEN"
  },
  "message": "connect ECONNREFUSED 127.0.0.1:8081",
  "name": "Error",
  "stack": "Error: connect ECONNREFUSED 127.0.0.1:8081\n    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)"
}

I assume this is because localhost points to whatever docker container is running the action. How can I get this request to go to my dev machine?

1

There are 1 answers

0
Dylan Landry On

After printing the value of process.env I found a property:

"__OW_API_HOST": "https://192.168.2.61",

At first I wasn't really sure what it was, but I tried out a request here and it worked:

axios.get('http://192.169.2.61:8081/api/health-check')

So maybe process.env.__OW_API_HOST contains an ip pointing to your local machine you can use.