CORS issue on netlify POST function

I have a netlify function that needs to be exposed to applications running on different domains.

The method returns the following response headers to allow cross domain access

  'Content-Type': 'application/json',
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Headers': 'Content-Type',
  'Access-Control-Allow-Methods': 'GET, HEAD, PUT, POST, OPTIONS, DELETE',

When invoked as a GET the endpoint returns an HTTP 405 response which is precisely what I would expect as the code dictates.

    if (httpMethod !== 'POST') {
      return { statusCode: 405, body: "Method Not Allowed" };

This endpoint is a POST. When invoked as a POST with a pay load I see the dreaded CORS error.

However when invoked as a POST with no payload the method succeeds. Well it responds with an HTTP 400 as it expects a payload, but no CORS error and that response is again function code dictated.

I have not the faintest idea what the issue could be here nor what form a resolution could take. I have tried implementing a PUT however the results are the same.

The payload is too large to use path parameters.

Seems the preflight requires an HTTP 204 response, so the following fixed it

    if (httpMethod === 'OPTIONS') {
      return getNoContentResponse();
    } else if (httpMethod !== 'POST') {
      return getMethodNotAllowedResponse();
1 Like