Returning empty response body from Netlify function triggers 502 error

Hi all !

I’m setting up a Netlify serverless function following the documentation here (Get started with functions | Netlify Docs) and am encountering an error when attempting to return a response without a body.

Minimal steps to reproduce:

  • Create function file in ./netlify/functions/my_function.js, to handle all incoming requests and return a simple response without body, i.e., a 302 redirect.

my_function.js:

export default async () => {
  return new Response("", {
    status: 302,
    headers: { Location: "https://newsite.com" },
  });
};
export const config = {
  path: "/*",
};

Once the function is deployed, every request to the site yields a response 502 Bad Gateway with a response body: error decoding lambda response: error decoding lambda response: unexpected end of JSON input.

The error disappears only if one replaces the first argument in the Response constructor with any non-empty string. This appears to be a bug in the Netlify API, since an empty HTTP response body is valid.

Thank you in advance for any light anyone could shed on this :).

1 Like

Hi :wave:t6: I see you wrote into our helpdesk. Please refer to ticket # 208801 if you have any further questions.

hi, for any others in this situation, the support team wrote to me to say that the solution is to use undefined in the Response constructor, rather than an empty string.

return new Response(undefined, { ...