cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get error fetch failed when i make a post request on localhost but wouldn't expect it

MarkusHobisch
Dynatrace Participant
Dynatrace Participant

Problem:

I have a node.js server running on localhost on port 3000. When I do a POST request in an app function I get an error. If I do the same request to remote url it works fine. 

Error I got:

 

 

 

 console.error
    response failed because:  TypeError: fetch failed
        at fetch (C:\Users\markus.hobisch\IdeaProjects\dynatrace-cpa\node_modules\undici\index.js:105:13)
        at processTicksAndRejections (node:internal/process/task_queues:95:5)
        at makePostRequest (C:\Users\markus.hobisch\IdeaProjects\dynatrace-cpa\api\collect-cloudwatch-logs-vpc-logs.ts:19:20)
        at fetchData (C:\Users\markus.hobisch\IdeaProjects\dynatrace-cpa\api\collect-cloudwatch-logs-vpc-logs.ts:67:16)
        at collectCloudwatchLogsVpcLogs (C:\Users\markus.hobisch\IdeaProjects\dynatrace-cpa\api\collect-cloudwatch-logs-vpc-logs.ts:89:22)
        at Object.<anonymous> (C:\Users\markus.hobisch\IdeaProjects\dynatrace-cpa\api\fetch-cloud-watch-logs.test.ts:34:54) {
      cause: Error: connect ECONNREFUSED ::1:3000
          at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1487:16)
          at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
        errno: -4078,
        code: 'ECONNREFUSED',
        syscall: 'connect',
        address: '::1',
        port: 3000
      }
    }

 

 

 

I added a screenshot with the error I got in debug UI as well.

I just made a simple post request to localhost and remote host. To produce the error you can use the code below:

 

 

 

const urlLocalhost = 'http://localhost:3000/api/cloudwatch-logs-simulator';
  const data = {
    logStreamName: 'name',
    startTime: 1675926120000,
    endTime: 1675926300000,
    nextToken: 'f/90/s',
    limit: 100,
  };

  const urlRemote = 'https://jsonplaceholder.typicode.com/posts';
  let response: Response;

  try {
    response = await makePostRequest(urlLocalhost, data);
  } catch (error) {
    console.error('response failed because: ', error);
    return;
  }

  const json = (await response.json()) as Promise<CollectCloudwatchLogsResponse>;
  return Promise.resolve(json);
}

 

 

 

 

 

 

 

const makePostRequest = async (url: string, data: any): Promise<Response> => {
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(data),
  });

  if (!response.ok) {
    throw new Error(`HTTP error! Status: ${response.status}`);
  }

  return response;
};

 

 

 

 

 

Cheers

Markus

2 REPLIES 2

christian_barth
Dynatrace Helper
Dynatrace Helper

Hi Markus, 

I assume want to call your local API from your locally running App, right? 

As soon as your app function is deployed, you can't reach localhost. 

The error you're getting is a generic "Connection refused" from node.

On which port you are running your app dev server? 

MarkusHobisch
Dynatrace Participant
Dynatrace Participant

Hi Christian,

I think I already solved my problem. The problem was that I used localhost instead of 127.0.0.1. This is a known bug in NodeJS 18. See here: ECONNREFUSED on NodeJS 18 · Issue #1624 · node-fetch/node-fetch · GitHub.
After I changed this my tests are green now. Thank you anyway for your response. 

Featured Posts