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. 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 = ',-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:
and not:

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

The 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: ',-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: is not in the cert's altnames:,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
        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: is not in the cert's altnames:,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,",
      host: '',
      cert: [Object],

Can you share the code you are trying to execute?