Back

Why am i getting CORS error from my client side when i have added Web platform from my Appwrite?

  • 2
  • Functions
  • Web
  • Cloud
Drake
8 Dec, 2023, 11:25

I suggest cutting down code until you have a successful response. Then, slowly add code until it breaks.

TL;DR
The user is experiencing a CORS error when trying to make a POST request from the client side to an Appwrite server. They have tried various solutions, including adding allow origins in CORS settings and deploying on Vercel, but the issue persists. They have also updated their function code and included CORS headers, but still get the error. The user is advised to research more about CORS and the appropriate server settings. Additionally, they are suggested to return the string representation of the error variable instead of the variable itself. The user is also told that sending an array buffer as a response may not work and should be converted to a string. Finally,
Drake
8 Dec, 2023, 11:26

Also, you probably can't send an array buffer back. It should be a string

bigsam
11 Dec, 2023, 09:43

this is my updated code and still getting CORS error trying to post request from client side

TypeScript
    try {
      const payload = querystring.parse(req.body);

      const pdfBuffer = await createPdf(payload);

      log(payload);

      const pdfBase64 = pdfBuffer.toString('base64');

      return res.send(pdfBase64, 200, { "Content-Type": "application/pdf" });
    } catch (err) {
      error('Error processing the request:', err);
      return res.send('Internal Server Error');
    }
  } else {
    return res.send('Bad Request');
  }

};```
D5
11 Dec, 2023, 10:15

This is a function code?

bigsam
11 Dec, 2023, 10:30

yes, the function code

D5
11 Dec, 2023, 10:47

Do you have set an API key?

bigsam
11 Dec, 2023, 10:51

yes i do have

D5
11 Dec, 2023, 13:00

What do you get if you return (res.send) the err variable?

bigsam
11 Dec, 2023, 13:32

please, can you explain what you meant by this?

Drake
11 Dec, 2023, 14:13

You still haven't returned the right CORS headers to tell the client they're allowed

bigsam
11 Dec, 2023, 14:20

i refactored it to this now

TypeScript
    try {
      const payload = JSON.parse(req.body);

      const pdfBuffer = await createPdf(payload);

      log(payload);

      const pdfBase64 = pdfBuffer.toString('base64');

      return res.send(pdfBase64, 200, { "Content-Type": "application/pdf" });
    } catch (err) {
      error('Error processing the request:', err);
      return res.send('Internal Server Error');
    }
  } else {
    log(error)
    return res.send('Bad Request');
  }```
bigsam
11 Dec, 2023, 14:22

still getting CORS error from the client side but it's completing/executing a successful function in my appwrite with "Bad request" response

Drake
11 Dec, 2023, 14:25

I suggest you research more into CORS and what servers should do to allow a client to make a request

bigsam
12 Dec, 2023, 09:59

i have been doing research on the CORS and tried every solution i came across but still doesn't work, please i need help!

bigsam
12 Dec, 2023, 10:00

i had even tried to deploy on vercel thinking it could that it's browser blocking localhost but still the same thing

Drake
12 Dec, 2023, 18:18

no...that's not how you handle CORS

Drake
12 Dec, 2023, 18:18

The server (your function) must tell the client what origins are allowed

bigsam
13 Dec, 2023, 09:59

how can i do this because i have tried all allow origins that i know of in CORS and it's not working and i can't find anything related to handling that in the appwrite docs

bigsam
13 Dec, 2023, 10:09

i have tried all these too

Drake
13 Dec, 2023, 23:23

Based on your research, what do you think you need to do?

bigsam
15 Dec, 2023, 15:38

that i need to handle cors and appwrite uses req to handle headers and all but i tried what i studied i was still getting cors error

bigsam
15 Dec, 2023, 15:39

i have used the client sdk instead...thanks

Drake
15 Dec, 2023, 16:09

how are you supposed to "handle cors"?

Drake
15 Dec, 2023, 16:10

it would be good to learn this concept

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