<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Avishka Ariyaratne on Medium]]></title>
        <description><![CDATA[Stories by Avishka Ariyaratne on Medium]]></description>
        <link>https://medium.com/@avishka95?source=rss-4ee228dd46ff------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Gpm6VJ1d4DbBBnhQ.jpg</url>
            <title>Stories by Avishka Ariyaratne on Medium</title>
            <link>https://medium.com/@avishka95?source=rss-4ee228dd46ff------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 16:03:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@avishka95/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[CORS and Effect: Understanding and Fixing CORS Errors for Good]]></title>
            <link>https://medium.com/@avishka95/cors-and-effect-understanding-and-fixing-cors-errors-for-good-06045a125122?source=rss-4ee228dd46ff------2</link>
            <guid isPermaLink="false">https://medium.com/p/06045a125122</guid>
            <category><![CDATA[api-development]]></category>
            <category><![CDATA[cors]]></category>
            <category><![CDATA[cors-error]]></category>
            <category><![CDATA[frontend-development]]></category>
            <dc:creator><![CDATA[Avishka Ariyaratne]]></dc:creator>
            <pubDate>Sun, 23 Jun 2024 17:26:00 GMT</pubDate>
            <atom:updated>2024-06-23T17:26:00.851Z</atom:updated>
            <content:encoded><![CDATA[<p>Ever heard someone say, “… works in Postman but fails in my browser”?</p><p>Maybe you’ve even experienced this yourself. You’re not alone. This is often one of the first challenges a frontend developer encounters in their career. Such issues arise because modern web browsers impose strict rules on network calls and APIs, unlike the more permissive environment of client applications such as <a href="https://www.postman.com/">Postman</a> or <a href="https://insomnia.rest/">Insomnia</a>.</p><h3>What is CORS? A Short and Comprehensive Explanation</h3><blockquote><em>A web page loaded in a browser is restricted from accessing an API that is not from the same domain/origin as the URL due to the </em><strong><em>Same-Origin Policy</em></strong><em> which is </em><strong><em>enforced by web browsers</em></strong><em> like Chrome and Safari. This policy was introduced to prevent malicious scripts on one site from accessing sensitive data on another site, such as in Cross-Site Request Forgery (CSRF) attacks. However, modern web applications often require more flexibility in accessing APIs from different origins.</em></blockquote><blockquote><strong><em>CORS (Cross-Origin Resource Sharing)</em></strong><em> is the established security protocol that enables APIs from different origins to be invoked by the web application of the browser under controlled conditions. These conditions are </em><strong><em>defined by the API server</em></strong><em> and communicated to the browser in the form of </em><strong><em>HTTP headers</em></strong><em> such as the following.</em></blockquote><blockquote>Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers, Access-Control-Allow-Credentials etc.</blockquote><h3>CORS in Action: A Simplified Step-by-Step Flow</h3><ol><li>The web application running on the browser initiates an API request.</li><li>For <strong>Simple Requests</strong> (straightforward requests) the browser makes the request with the Origin header. For <strong>Preflighted Requests</strong> (more complex requests), the browser first sends an OPTIONS preflight request to determine the safety of it.</li><li>Preflight Request — An OPTIONS request is sent with the Origin header along with Access-Control-Request-Method and Access-Control-Request-Headers indicating the method and custom headers of the actual request.</li><li>The server responds to the Preflight Request with <a href="https://developer.mozilla.org/en-US/docs/Glossary/CORS#cors_headers">CORS headers</a>. The browser evaluates the response; if the headers allow the request, the browser proceeds with the actual request. If not, the browser blocks the request.</li><li>For simple requests or after a successful preflight response, the browser sends the actual request to the server.</li><li>The server’s response is then evaluated and delivered to the web application only if it is allowed.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1013/1*KFz5lvUt58myNqyii29eJA.png" /><figcaption>CORS Step-by-Step Flow</figcaption></figure><h3>Troubleshooting CORS</h3><p>To troubleshoot CORS-related issues, resources like <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors">MDN</a> are incredibly helpful. The CORS policy is enforced by the browser, based on the response headers sent by the server (API). Therefore, <strong>the first place to check is the response sent by the server.</strong></p><p>To demonstrate troubleshooting, we will focus on two common CORS-related errors involving the popular CORS header, Access-Control-Allow-Origin.</p><h4>Errors</h4><p><strong>CORS header </strong><strong>Access-Control-Allow-Origin does not match </strong><strong>https://localhost:3000</strong></p><ul><li><strong>Main reason:</strong> The origin (e.g.<a href="https://localhost:3000/">https://localhost:3000</a>) does not match the origin shown in the Access-Control-Allow-Origin header returned by the server.</li><li><strong>Less likely reason:</strong> Server response includes more than one Access-Control-Allow-Origin header.</li></ul><p><strong>CORS header </strong><strong>Access-Control-Allow-Origin missing</strong></p><ul><li><strong>Main reason:</strong> The Access-Control-Allow-Origin header is not being returned by the server.</li></ul><h4>Solution</h4><p>You need to specify either all origins by adding the wildcard character * or only the requesting origin (e.g., https://localhost:3000) in the Access-Control-Allow-Origin response header.</p><ul><li>Access-Control-Allow-Origin: * or</li><li>Access-Control-Allow-Origin: <a href="https://localhost:3000/">https://localhost:3000</a></li></ul><p>In addition, ensure duplicate headers are not being sent.</p><h3>CORS Issues in Local Environments</h3><p>In production, CORS headers are managed through web servers (like Apache or NGINX), load balancers, reverse proxies, or API gateways. During local development, CORS issues are common. You should use middleware, proxies, or language-specific configurations (e.g., Express.js for Node.js, Flask for Python, Spring Boot for Java) to enable these headers. Some developers temporarily bypass these security mechanisms using browser extensions or by disabling browser security, but this should only be done for development due to security risks.</p><p>I have written a simple proxy server to handle most CORS needs during development. You can check it out here <a href="https://github.com/avishka95/cors-proxy-server">https://github.com/avishka95/cors-proxy-server</a></p><h3>Additional Keywords</h3><p><strong>Same Origin Policy</strong></p><figure><img alt="An example URL of https://localhost:3000 with it’s components highlighted." src="https://cdn-images-1.medium.com/max/1024/1*rH3Z0aWKhcixqAiyJJewhA.png" /><figcaption>Example URL</figcaption></figure><p>An origin is defined by the combination of a URI scheme (protocol), hostname, and port. Two URLs share the same origin only if all three of these components are exactly the same. <a href="https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy">Read more</a></p><p><strong>Simple request</strong></p><p>These are straightforward API requests that meet certain criteria defined by the CORS protocol. They use one of the HTTP methods GET, HEAD, or POST, and their headers are limited to Accept, Accept-Language, Content-Language, and Content-Type (with values of application/x-www-form-urlencoded, multipart/form-data, or text/plain).</p><p><strong>Preflighted Requests</strong></p><p>A request becomes a preflighted request in CORS when it does not meet the criteria for a simple request. This includes requests with non-simple methods (such as PUT, DELETE, etc.), requests that use custom headers, or requests with a Content-Type header that is not one of application/x-www-form-urlencoded, multipart/form-data, or text/plain. Additionally, requests that include credentials (withCredentials or credentials: &#39;include&#39;) also trigger a preflight check. In such cases, the browser sends an OPTIONS preflight request to the server to check if the actual request is safe to send.</p><h3>That’s CORS in a Nutshell</h3><p>With the insights gained from this article, managing the complexities of CORS should be much easier for you. Best of luck with your development!</p><pre><strong>Further Reading</strong><br><a href="https://www.w3.org/TR/2020/SPSD-cors-20200602">Cross-Origin Resource Sharing<br>W3C Recommendation 16 January 2014, superseded 2 June 2020 - W3C</a><br>https://www.w3.org/TR/2020/SPSD-cors-20200602<br><br><a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS">Cross-Origin Resource Sharing (CORS) - MDN Web Docs</a><br>https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS<br><br><a href="https://aws.amazon.com/what-is/cross-origin-resource-sharing">What is Cross-Origin Resource Sharing (CORS)? - AWS</a><br>https://aws.amazon.com/what-is/cross-origin-resource-sharing</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=06045a125122" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>