Replies: 3 comments 2 replies
|
|
|
Not a dumb question at all! The CORS failure is expected behavior — the desktop app's Electron shell enforces standard web security ( The fix: use a backend script instead of a frontend script. Backend scripts run in Node.js (server-side), so they're not subject to browser CORS at all. Set your script's MIME type to const response = await fetch('https://external-api.com/endpoint', {
headers: { 'x-api-user': 'your-user' }
});
const data = await response.json();
return data;If you need to trigger the call from a UI context (e.g. a button in a frontend script), you can delegate to the backend with const result = await api.runOnBackend(async (url, apiUser) => {
const resp = await fetch(url, {
headers: { 'x-api-user': apiUser }
});
return await resp.json();
}, ['https://external-api.com/endpoint', 'your-user']);This is why your Python/JupyterLab calls work fine — they're also server-side and not subject to browser CORS policies. To reply, just mention @dosu. Share context across your team and agents. Try Dosu. |
|
Two separate things changed since that answer, and the error text tells you which one you are hitting. If the error mentions If instead you are getting a 403 from Nominatim, that is the frontend versus backend difference you noticed, and it is about the User-Agent. Node's native Setting one fixes it, but there is a trap in how you set it. Measured against the live endpoint, spacing requests out so rate limiting was not a factor: Any User-Agent containing an email address is refused. Parentheses make no difference and a URL is fine, so it is the address itself. That is worth knowing because putting a contact email in the User-Agent is the usual way people follow the "identify your application" instruction, and here it is the one thing that guarantees a block. So in a backend script: const res = await fetch(url, { headers: { "User-Agent": "trilium-myscript/1.0 (https://your-site.example)" } });Worth reading the policy itself before leaning on the public endpoint for anything regular, since it also caps you at one request per second and forbids a few use cases outright: https://operations.osmfoundation.org/policies/nominatim/ |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
maybe a dumb question, but my
fetch(url, options)requests in frontend JS scripts won't work because of CORS issues.If the GET request does not need headers, then it works fine (with what I tested), but as soon as I need custom headers like : "x-api-user" for instance, then it fails with
. This happens whether I add the 3 AccessControlAllow headers in the
options.headersor not.LLMs are all suggesting proxies and not intuitive stuff and yet, the same calls, in python, using jupyterlab in my Brave browser, reach the server successfully.
I'd like to perform this using a Desktop version, so I only played with the Desktop env variables, either at launch using a bat, or in the config.ini (testing it on 1.103.0 and 1.104.0). But should I also change the variables in my server config, in the docker-compose.yml (wouldn't make sense to me, but still asking) ?
All reactions