Netlify Function returns "502 Bad Gateway" error

Site Name: gracious-franklin-e35b33

Netlify Function throws a 502 Bad Gateway error when requested using its public url e.g. https://www.abc.com/.netlify/functions/my-fn. I can tell the function is executed from Netlify’s servers because of the presence of log messages I added. No errors in the logs either.

The code is simple - please refer below. What am I missing here?

export default async (req: Request) => {
  const headers = new Headers({ 'Access-Control-Allow-Origin': '*' });
  const url = 'https://api.weather.gov/points/39.7456,-97.0892';

  try {
    const resp = await fetch(url, { headers: req.headers });
    const data = await resp.json();
    console.log({ url, data }); // THIS WORKS
    return Response.json({ data }, { headers });
  } catch (error) {
    console.log({ url, headers, error });
    return Response.json({ error: "No available data" }, { headers });
  }
};

Howdy @swaaWu2YQ and welcome to the community!

The function endpoint for site gracious-franklin-e35b33 should be: https://www.curlito.com/.netlify/functions/og
and not:
https://www.abc.com/.netlify/functions/my-fn

Are you sure you’re invoking the right function? :thinking:

The abc.com link I posted was just an example pattern as I’m not sure if viewers will have access to my account. But yes, I’m invoking the right function URL (see screenshot) and the Netlify console logs I added confirm that the function indeed executes correctly - albeit with the 502 error.

The error is this:

error decoding lambda response: error decoding lambda response: invalid character '\x00' after top-level value

Could you try to use the Functions API v1 (Lambda compatibility version), to see if you get the same error?

@hrishikesh that fixed it - after I switched to the Lambda compatibility for Functions | Netlify Docs and the appropriate Response object. Thanks.

Can you give some insight for future reference as to why I have to use the API v1 and not the newer version?

Since it works with v1, I think this could be a bug with v2. I’ll file it for the devs.

@swaaWu2YQ I have tried the exact same code on my test site and it works. But you get to the error as that API does not accept request from your domain:

Nov 28, 03:55:38 PM: b5fd6dea INFO   {
  url: 'https://api.weather.gov/points/39.7456,-97.0892',
  headers: HeadersList {
    cookies: null,
    [Symbol(headers map)]: Map(1) { 'access-control-allow-origin' => [Object] },
    [Symbol(headers map sorted)]: null
  },
  error: TypeError: fetch failed
      at Object.fetch (node:internal/deps/undici/undici:11576:11)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async Module.hello_default (file:///var/task/hello.mjs:15:18)
      at async Runtime.handler (file:///var/task/___netlify-bootstrap.mjs:1851:15)
      at async Runtime.handleOnceStreaming (file:///var/runtime/index.mjs:1206:26) {
    cause: Error [ERR_TLS_CERT_ALTNAME_INVALID]: Hostname/IP does not match certificate's altnames: Host: com-200--poetic-pothos-6eab9b.netlify.app. is not in the cert's altnames: DNS:weather.gov, DNS:airquality.weather.gov, DNS:alerts-v2.weather.gov, DNS:alerts.weather.gov, DNS:amdar.ncep.noaa.gov, DNS:amdar.noaa.gov, DNS:api-v1.weather.gov, DNS:api.weather.gov, DNS:aviationweather.gov, DNS:cfs.ncep.noaa.gov, DNS:digital.weather.gov, DNS:f1.weather.gov, DNS:forecast-v3.weather.gov, DNS:forecast.weather.gov, DNS:gisc-washington.ncep.noaa.gov, DNS:graphical.weather.gov, DNS:hads.ncep.noaa.gov, DNS:hysplit.ncep.noaa.gov, DNS:inws.ncep.noaa.gov, DNS:iris.ncep.noaa.gov, DNS:madis-data.ncep.noaa.gov, DNS:madis.ncep.noaa.gov, DNS:madis.noaa.gov, DNS:mag.ncep.noaa.gov, DNS:magpara.ncep.noaa.gov, DNS:marine.weather.gov, DNS:mobile.weather.gov, DNS:nomads.ncep.noaa.gov, DNS:nomads.weather.gov, DNS:nws.weather.gov, DNS:opengeo.ncep.noaa.gov, DNS:preview-alerts.weather.gov, DNS:preview-api.weather.gov, DNS:preview-forecast-v3.weather.gov, DNS:preview-forecast.weather.gov, DNS:preview-idp.weather.gov, DNS:preview-idpgis.ncep.noaa.gov, DNS:preview-marine.weather.gov, DNS:preview-opengeo.ncep.noaa.gov, DNS:preview-ra4-gifs.weather.gov, DNS:preview-radar.weather.gov, DNS:preview-spot.weather.gov, DNS:preview.weather.gov, DNS:products.weather.gov, DNS:ptwc.weather.gov, DNS:ra4-gifs.weather.gov, DNS:radar-v2.weather.gov, DNS:radar.weather.gov, DNS:radar2pub.ncep.noaa.gov, DNS:radar3pub.ncep.noaa.gov, DNS:ripcurrents.noaa.gov, DNS:rsmc.ncep.noaa.gov, DNS:ssd.wrh.noaa.gov, DNS:test-nomads.ncep.noaa.gov, DNS:tsunami.gov, DNS:w1.weather.gov, DNS:w2.weather.gov, DNS:water.weather.gov, DNS:www.aviationweather.gov, DNS:www.cpcpara.ncep.noaa.gov, DNS:www.lib.ncep.noaa.gov, DNS:www.ncep.noaa.gov, DNS:www.nco.ncep.noaa.gov, DNS:www.ndsc.ncep.noaa.gov, DNS:www.nws.noaa.gov, DNS:www.opah.ncep.noaa.gov, DNS:www.prh.noaa.gov, DNS:www.ripcurrents.noaa.gov, DNS:www.srh.noaa.gov, DNS:www.tsunami.gov, DNS:www.tsunami.noaa.gov, DNS:www.vwt.ncep.noaa.gov, DNS:www.weather.gov, DNS:www.wrh.noaa.gov, DNS:wwwx.wrh.noaa.gov
        at new NodeError (node:internal/errors:405:5)
        at Object.checkServerIdentity (node:tls:337:12)
        at TLSSocket.onConnectSecure (node:_tls_wrap:1610:27)
        at TLSSocket.emit (node:events:517:28)
        at TLSSocket._finishInit (node:_tls_wrap:1017:8)
        at ssl.onhandshakedone (node:_tls_wrap:803:12) {
      reason: "Host: com-200--poetic-pothos-6eab9b.netlify.app. is not in the cert's altnames: DNS:weather.gov, DNS:airquality.weather.gov, DNS:alerts-v2.weather.gov, DNS:alerts.weather.gov, DNS:amdar.ncep.noaa.gov, DNS:amdar.noaa.gov, DNS:api-v1.weather.gov, DNS:api.weather.gov, DNS:aviationweather.gov, DNS:cfs.ncep.noaa.gov, DNS:digital.weather.gov, DNS:f1.weather.gov, DNS:forecast-v3.weather.gov, DNS:forecast.weather.gov, DNS:gisc-washington.ncep.noaa.gov, DNS:graphical.weather.gov, DNS:hads.ncep.noaa.gov, DNS:hysplit.ncep.noaa.gov, DNS:inws.ncep.noaa.gov, DNS:iris.ncep.noaa.gov, DNS:madis-data.ncep.noaa.gov, DNS:madis.ncep.noaa.gov, DNS:madis.noaa.gov, DNS:mag.ncep.noaa.gov, DNS:magpara.ncep.noaa.gov, DNS:marine.weather.gov, DNS:mobile.weather.gov, DNS:nomads.ncep.noaa.gov, DNS:nomads.weather.gov, DNS:nws.weather.gov, DNS:opengeo.ncep.noaa.gov, DNS:preview-alerts.weather.gov, DNS:preview-api.weather.gov, DNS:preview-forecast-v3.weather.gov, DNS:preview-forecast.weather.gov, DNS:preview-idp.weather.gov, DNS:preview-idpgis.ncep.noaa.gov, DNS:preview-marine.weather.gov, DNS:preview-opengeo.ncep.noaa.gov, DNS:preview-ra4-gifs.weather.gov, DNS:preview-radar.weather.gov, DNS:preview-spot.weather.gov, DNS:preview.weather.gov, DNS:products.weather.gov, DNS:ptwc.weather.gov, DNS:ra4-gifs.weather.gov, DNS:radar-v2.weather.gov, DNS:radar.weather.gov, DNS:radar2pub.ncep.noaa.gov, DNS:radar3pub.ncep.noaa.gov, DNS:ripcurrents.noaa.gov, DNS:rsmc.ncep.noaa.gov, DNS:ssd.wrh.noaa.gov, DNS:test-nomads.ncep.noaa.gov, DNS:tsunami.gov, DNS:w1.weather.gov, DNS:w2.weather.gov, DNS:water.weather.gov, DNS:www.aviationweather.gov, DNS:www.cpcpara.ncep.noaa.gov, DNS:www.lib.ncep.noaa.gov, DNS:www.ncep.noaa.gov, DNS:www.nco.ncep.noaa.gov, DNS:www.ndsc.ncep.noaa.gov, DNS:www.nws.noaa.gov, DNS:www.opah.ncep.noaa.gov, DNS:www.prh.noaa.gov, DNS:www.ripcurrents.noaa.gov, DNS:www.srh.noaa.gov, DNS:www.tsunami.gov, DNS:www.tsunami.noaa.gov, DNS:www.vwt.ncep.noaa.gov, DNS:www.weather.gov, DNS:www.wrh.noaa.gov, DNS:wwwx.wrh.noaa.gov",
      host: 'com-200--poetic-pothos-6eab9b.netlify.app',
      cert: [Object],
      code: 'ERR_TLS_CERT_ALTNAME_INVALID'
    }
  }
}

Can you share the code you are trying to execute?