#[CLOSED] CORS error using the Locale API from a browser: CORS Allow Origin Not Matching Origin

17 messages · Page 1 of 1 (latest)

runic echo
#

When I call the GET /v1/locale API with JS in a browser the GET request fails with CORS Allow Origin Not Matching Origin.

Requests to GET /v1/databases/... work fine and return the correct access-control-allow-origin header (domain.com), but the /v1/locale returns the Appwrite url only (api.domain.com).
The OPTION request for the locale also succeeds with the correct header.

I have added the domain as a platform in the console.

drowsy rover
runic echo
drowsy rover
#

Can you share the code with me?

#

Only the part where you're handling this

#

And have you added your domain in the platform?

runic echo
#

The response headers for the OPTIONS request are domain.com for both, it's just the GET request that is different

drowsy rover
# runic echo Here's the code: https://pastebin.com/TgLCanTc And yes it's added as a Web plat...

You have an issue in your code (as per what I know about initializing clients) -
In this part:

let client = null;
let databases = new Databases(client);
let locale = new Locale(client);

you are constructing the DB & Locale with client as null.

Then again in this:

export function initClient(config, sessionToken = null) {
  client = new Client()
    .setEndpoint(config.public.appwriteEndpoint)
    .setProject(config.public.appwriteProjectId);
  if (sessionToken != null) {
    client.setSession(sessionToken);
  }
 
  databases = new Databases(client);
  locale = new Locale(client);
}

you are re-defining them. But what's going wrong is that when the part let client... is read, it supposes you've your client undefined. Which causes issues. So please fix your code like this and check whether it works:

import { Client, Databases, Locale } from "appwrite";

let client = null;
let databases = null;
let locale = null;

export function initClient(config, sessionToken = null) {
  client = new Client()
    .setEndpoint(config.public.appwriteEndpoint)
    .setProject(config.public.appwriteProjectId);

  if (sessionToken) client.setSession(sessionToken);

  databases = new Databases(client);
  locale = new Locale(client);
}

export function getLocale() {
  return locale;
}

export function getDatabases() {
  return databases;
}
runic echo
#

I changed it to what you wrote but still get the CORS error

drowsy rover
#

And one more thing, can you please also share the Console & Network output?

runic echo
#

Yes, the endpoint is only set once when the initClient function is called the first time (and all other Appwrite calls succeed, just not the Locale one) 🙂

https://pastebin.com/E9QKkbyN

runic echo
#

I deleted the platform and added it back and restarted the appwrite containers and now it suddenly works without issues

runic echo
#

No, the locale call returns {"ip":"172.18.0.1","countryCode":"--","country":"Unknown","continentCode":"--","continent":"Unknown","eu":false,"currency":""}, just need to forward the IP from Nginx 🙂