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

Dynatrace Participant
Dynatrace Participant


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:




    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 = '';
  let response: Response;

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

  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;









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? 

Dynatrace Participant
Dynatrace Participant

Hi Christian,

I think I already solved my problem. The problem was that I used localhost instead of 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