<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: M Hussam </title>
    <description>The latest articles on Forem by M Hussam  (@hussam13).</description>
    <link>https://forem.com/hussam13</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1014501%2F88ea01d1-34de-4c29-bdfe-724a118e3ec3.jpeg</url>
      <title>Forem: M Hussam </title>
      <link>https://forem.com/hussam13</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hussam13"/>
    <language>en</language>
    <item>
      <title>CORS issue in Ruby on Rails(Duplicate Response Headers)</title>
      <dc:creator>M Hussam </dc:creator>
      <pubDate>Wed, 25 Jan 2023 13:08:21 +0000</pubDate>
      <link>https://forem.com/hussam13/cors-issue-in-ruby-on-railsduplicate-response-headers-333p</link>
      <guid>https://forem.com/hussam13/cors-issue-in-ruby-on-railsduplicate-response-headers-333p</guid>
      <description>&lt;p&gt;Any rails developer who writes an API will counter the problem regarding the Cors. For those who don't know, CORS (Cross-origin resource sharing) is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain.&lt;/p&gt;

&lt;p&gt;While using Nginx and Rails gem Rack-cors, chances are high you are getting a problem of duplicate response header values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Access-Control-Allow-Origin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It refers to the origin from where the request is made and the asterisk shows, it allows a response from any domain.&lt;/p&gt;

&lt;p&gt;Access-Control-Allow-Origin: *&lt;/p&gt;

&lt;p&gt;It is allowed only at once in the response header. As The CORS spec explicitly states:&lt;/p&gt;

&lt;p&gt;“ If the response includes zero or more than one Access-Control-Allow-Origin header value, return fail and terminate this algorithm. ”&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is the problem?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Access-Control-Allow-Origin is provided by both Rails rack-cors and Nginx. The problem occurs when Nginx provides a configuration that doesn’t override the one provided by Rails and gets duplicated.&lt;/p&gt;

&lt;p&gt;It shows the error like,&lt;/p&gt;

&lt;p&gt;“Access to XMLHttpRequest at (…) from the origin (…) has been blocked by CORS policy. The header contains multiple values ‘*, *’, but only one is allowed.”&lt;/p&gt;

&lt;p&gt;And you get Response Headers values duplicated such as,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkz14xd6uf0komq042zhm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkz14xd6uf0komq042zhm.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then you are on the right way to solving the problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Both Rails rack-cors and Nginx provides a way to setting up the access Access-Control-Allow-Origin. There is nothing crossed which overcomes this problem of not overriding the values.&lt;/p&gt;

&lt;p&gt;There can be many solutions you can opt for but there is one which I recommend is only to set up response header from rails using rack-cors gem. And commenting the response headers from the Nginx configuration file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy0fmuq74bxturt2siowv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy0fmuq74bxturt2siowv.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because rails rack-cors can come in handy in making changes instead of Nginx configuration.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>cors</category>
      <category>nginx</category>
    </item>
  </channel>
</rss>
