-
Notifications
You must be signed in to change notification settings - Fork 334
Cors blog #541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cors blog #541
Changes from 11 commits
3927d1e
b6540a5
8b863fd
faf83bf
13cec30
65998fb
5d4f98a
69273aa
6dca5c5
e212c3b
216ff9a
4997404
245852e
c0c8ef5
ddcf533
0e37aa3
b67dab2
643556a
8609acc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,64 @@ | ||||||
| --- | ||||||
| layout: post | ||||||
| title: Solving CORS errors with appwrite | ||||||
| description: Understanding why you are getting CORS error when sending request to an appwrite backend, and how to debug | ||||||
| date: 2024-01-16 | ||||||
| cover: /images/blog/cors-error/cors_cover.png | ||||||
| timeToRead: 3 | ||||||
| author: dennis-ivy | ||||||
| category: engineering | ||||||
| featured: false | ||||||
| --- | ||||||
|
|
||||||
| I want to address an issue I've seen popping up on stack overflow and the appwrite discord server and address some of the reasons you may be getting this error, and walk you through some of the steps you can take to try and resolve it as well. | ||||||
|
|
||||||
| The error message you'll see in your console when trying to make a request to an appwrite backend will look something like this: `Access blocked by CORS policy` | ||||||
|
|
||||||
| ## **Understanding CORS** | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We start with |
||||||
|
|
||||||
| Before we start debugging this, let's talk about what CORS error is. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The following section teaches the reader what CORs is before referring to the error.
Suggested change
|
||||||
|
|
||||||
| Without diving deep into the topic, CORS (Cross-Origin Resource Sharing ) is a mechanism which allows a server to specify which origins can access it. By origins I mean URL. Basically, site A, our server sitting at `myapi.com` won't allow request coming from site B, our client sitting at `myfrontend.com`. | ||||||
|
|
||||||
| This happens because our server has not added site B, `myfrontend.com` to it's list of allowed origins, therefor any request coming from a URL that is not listed in our servers allowed origins will be rejected by our CORS policy. The solution in this case would be to simply add `myfrontend.com` to the list of allowed origins. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
|
|
||||||
|  | ||||||
|
|
||||||
| CORS is crucial because it provides a secure way to make request accross different origins. Without CORS, any website would be able to make request to our server, and would lead to major problems. | ||||||
|
|
||||||
|
|
||||||
| ## Why you are getting a CORS error | ||||||
|
|
||||||
| Now let's try to figure this all out in the context of appwrite and why you may be getting this error. At the moment I have this listed at 3 main reasons, if more arise I will update the article to include them. | ||||||
|
|
||||||
| 1. Origin not set in console | ||||||
| 2. Origin is set incorrectly | ||||||
| 3. Bad ID on request | ||||||
|
|
||||||
| ### 1 - Origin not set in console | ||||||
|
|
||||||
| First, you'll want to check your appwrite console to make sure you have added a hostname and are making a request from the correct hostname. Make sure you have added a platform in your appwrite console by going to the "overview" tab, select your platform (Add one if you have not yet) and then ensure you have added a hostname. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. capatilize Appwrite and Console |
||||||
|
|
||||||
| A hostname is simply the domain you will be making request from. In development this will most likely be `localhost`, no need to add a port number or protocal here. | ||||||
|
|
||||||
| ### 2 - Origin is set incorrectly | ||||||
|
|
||||||
| The second issue I often see is a hostname thats not properly configured. This is usually a typo, or more often a mistake made when switching between a local domain and live domain. Developers will often deploy their website and wonder why they are getting this error, only to find they still have their hostname set as localhost. | ||||||
|
|
||||||
| So if you find this is why you were getting a CORS error, you have a few ways of solving this. | ||||||
|
|
||||||
| 1. Update hostname each time you switch between localhost and live url. This is not ideal since you would be switching back and fourth constantly | ||||||
| 2. Add a wildcard to allow request from any origin - Not secure | ||||||
| 3. Add multiple origins - This can be done by adding another "platform" and simply specifying the second and third origins as you add them. - Recommended | ||||||
|
|
||||||
| ### 3 - Incorrect ID on request | ||||||
|
|
||||||
| This one happens because of a improperly configured request, such as a typo when specifying a project ID. For example, when using the `listDocuments` method, if the project ID is set incorrectly when the client was initialized, you will receive a CORS error. | ||||||
|
|
||||||
| Without diving into the details about how CORS works, the problem occurs when the browser tries to check if the origin is allowed. The request returns a 40X response so the entire CORS check fails. | ||||||
|
|
||||||
| ### Other things to consider | ||||||
|
|
||||||
| In most cases the issues people face have to do with one of the above reasons listed and can be solved with the given suggestions. However, if you are still running into issues I’ll keep an ongoing list of other possibilities and things to check for. | ||||||
|
|
||||||
| - Disabled CORS in browser. (I’ve seen people have this issue with browser extensions) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add another line with resources:
|
||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: Server
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch 😆
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! I fixed it in the video before release but forgot to update the blog image. I agree with the font issue. I'm using Excalidraw at the moment so I'll see if I can add custom fonts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Capitalize
Stack OverflowAppwriteandDiscordThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@divanov11 Take a look here at Laura's comments.