1

I am working on a microservice which tries to access a url. Headers for Authorization and Content-type is set. NPM is working fine.

Using curl to access the same url with Authorization header also gives the result. But I'm getting this error while hitting the url via the complete code.

"message":"3. Error while making request - Error: tunneling socket could not be established, cause=connect EINVAL 0.0.12.56:80 - Local (0.0.0.0:0)","level":"error"}

The code uses the request-promise-native module

const request = require('request-promise-native')
const logger = require(process.env.APP_SERVICES_DIR + 'logger');

function getRequestAPI(task) {

  console.log(`Basic ${Buffer.from(
    `${process.env.USERNAME}:${process.env.PASSWORD}`
  ).toString('base64')}`);

  return {
    url: getQueryForTask(task),
    headers: {
      Authorization: `Basic ${Buffer.from(
        `${process.env.USERNAME}:${process.env.PASSWORD}`
      ).toString('base64')}`,
      'Content-Type': 'application/json'
    },
    resolveWithFullResponse: true
  };
}


function makePortalRequest(task) {
  logger.info(`Making request for task ${task.task_key}`);

  return request(getRequestAPI(task))
    .then(response => {
      // logger.info('Response received');
      console.log("get request API" );
      try {
        const responseBody = JSON.parse(response.body);

        // Parse result and store
        return parseResultAndStore(responseBody, task).then(() => {
          return {
            success: true
          };
        });
      } catch (e) {
        logger.error(
          `Fetch Task: Error in parsing response from portal - ${e.message}`,
          e
        );
        return {
          success: false,
          message: e.message
        };
      }
    })
    .catch(e => {
      let errResponse = e.response || e.message;

      if (errResponse.body) {
        let responseBody = '';
        // wrap JSON parse in try catch
        try {
          responseBody = JSON.parse(errResponse.body);
          errResponse = `${
            errResponse.statusMessage
          } : ${responseBody.errorMessages.toString()}`;
          logger.error(`1. Error while making request - ${errResponse}`);
        } catch (e) {
          // if JSON parse fails log the string
          logger.error(
            `2. Error while making request - ${errResponse.body}`
          );
          errResponse = errResponse.body;
        }
      } else {
        logger.error(`3. Error while making request - ${errResponse}`);
      }

      return {
        success: false,
        message: errResponse
      };
    });
}

This service runs in a docker container. Service components are described by a docker-compose file and all environment variables are set appropriately. By running the command below I can open bash inside docker container.

docker-compose exec run service-name bash

The curl command is able to access the url from inside the docker bash.

curl -X GET 'https://url-to-fetch/?params=xyz' -H 'Authorization: Basic aDEwMjZzNzpXaXB0ZWxAMjAxOF8='

But in the running code, I'm not able to get output response from the url.

"message":"3. Error while making request - Error: tunneling socket could not be established, cause=connect EINVAL 0.0.12.56:80 - Local (0.0.0.0:0)","level":"error"}

How to resolve this problem? What might be the possible reasons for this error?

sks147
  • 196
  • 2
  • 8

0 Answers0