Skip to content
Init is coming / May 19 - 23
Back

Getting Cors Error after 2 weeks

  • 1
  • Functions
  • Cloud
ANIKET
17 May, 2025, 07:21

TLDR: I've deployed a appwrite function and adding appropriate headers while sending the request back and it was working well and good previous week but today suddely the request started to fail and i started getting CORS error, Also idk why but the GET request is working but not the POST.

Here's the code (Also i want to make sure the functions is accessible by the public users)

TypeScript
export default async ({ req, res, log, error }) => {
  const corsHeaders = {
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
    'Access-Control-Allow-Headers': 'Content-Type, Authorization'
  };

  if (req.method === 'OPTIONS') {
    return res.json({}, 200, corsHeaders);
  }

  if (req.method === 'GET') {
    try {
      return res.json({"message":"Triggered the pipeline"}, 200, corsHeaders);
    } catch (err) {
      console.error('GET request error:', err);
      return res.json({ error: 'Internal Server Error' }, 500, corsHeaders);
    }
  }

  if (req.path === "/health_check") {
    return res.json({"status":"Healthy"}, 200, corsHeaders);
  }

  if (req.method === 'POST') {
    try {
      const body = req.body || {};
      let payload;
      
   // actual code 

      return res.json({
        message: '... ... successfully',
      }, 200, corsHeaders);

    } catch (err) {
      console.error('Error ...:', err);
      return res.json({ 
        error: 'Internal Server Error', 
        message: err.message 
      }, 500, corsHeaders);
    }
  }

  return res.json({ error: 'Method not allowed' }, 405, corsHeaders);
};

Thanks

TL;DR
Developers added appropriate CORS headers for an appwrite function. GET request is working, but POST is failing with CORS error suddenly. To ensure function accessibility by the public, check CORS headers and if POST request body is correctly handled.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more