Home
Support Forums

Netlify functions POST request CORS error

I’m getting CORS Preflight error and it says that No 'Access-Control-Allow-Origin' header is present on the requested resource. . The problem is that it’s actually there, I had the same problem with my GET request, added the headers and everything works, but for POST request it still returns the CORS error.

Here’s my handler and headers

const headers = {
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Headers': 'Content-Type',
  'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
};

exports.handler = async function (event, context) {
  ip = event.headers['client-ip'];

  const request = JSON.parse(event.body);
  const respond = await authenticate((auth) =>
    addEvent(auth, request.name, request.dateTime, request.email, request.info)
  );

  return {
    statusCode: respond.status,
    body: JSON.stringify(respond),
    headers,
  };
};

Hi @markedway,

How can we trigger the function to test?

Also, I notice you’re using a service worker. Have you configured it correctly to ignore the POST requests? If not, chances are it’s the service worker actually responding to your requests.

Side note: If you’re trying to get the IP address of the user, that’s not how you’d get it. client-ip is not the IP address of the user.