<?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>DEV Community: Alireza Hassankhani</title>
    <description>The latest articles on DEV Community by Alireza Hassankhani (@alireza_hassankhani_b8401).</description>
    <link>https://dev.to/alireza_hassankhani_b8401</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4019709%2Fc8197511-bc55-491a-88a6-31a212b7d813.png</url>
      <title>DEV Community: Alireza Hassankhani</title>
      <link>https://dev.to/alireza_hassankhani_b8401</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alireza_hassankhani_b8401"/>
    <language>en</language>
    <item>
      <title>What Is a Simple Request and When Does the Browser Send a Preflight Request?</title>
      <dc:creator>Alireza Hassankhani</dc:creator>
      <pubDate>Wed, 15 Jul 2026 11:59:10 +0000</pubDate>
      <link>https://dev.to/alireza_hassankhani_b8401/what-is-a-simple-request-and-when-does-the-browser-send-a-preflight-request-2a9b</link>
      <guid>https://dev.to/alireza_hassankhani_b8401/what-is-a-simple-request-and-when-does-the-browser-send-a-preflight-request-2a9b</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/alireza_hassankhani_b8401/understanding-cors-preflight-requests-4bf"&gt;previous article&lt;/a&gt;, we learned that browsers sometimes send an &lt;code&gt;OPTIONS&lt;/code&gt; request before the actual Cross-Origin request. This process is known as a &lt;strong&gt;CORS Preflight Request&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But does this happen for every request?&lt;/p&gt;

&lt;p&gt;The answer is &lt;strong&gt;no&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Browsers only perform a Preflight Request when the request does &lt;strong&gt;not&lt;/strong&gt; qualify as a &lt;strong&gt;Simple Request&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What Is a Simple Request?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Simple Request is a Cross-Origin request that satisfies a specific set of conditions defined by the Fetch specification.&lt;/p&gt;

&lt;p&gt;Since these requests are considered low risk, the browser sends them directly without performing a Preflight check.&lt;/p&gt;

&lt;p&gt;CORS rules still apply to the response, but no preliminary &lt;code&gt;OPTIONS&lt;/code&gt; request is needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Requirements for a Simple Request&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A request is considered simple only if &lt;strong&gt;all&lt;/strong&gt; of the following conditions are met.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. HTTP Method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The request method must be one of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;GET&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HEAD&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;POST&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Methods such as &lt;code&gt;PUT&lt;/code&gt;, &lt;code&gt;PATCH&lt;/code&gt;, and &lt;code&gt;DELETE&lt;/code&gt; automatically trigger a Preflight Request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Request Headers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The request may only include CORS-safelisted request headers, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Accept&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Accept-Language&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Content-Language&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Adding headers like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;Authorization: Bearer &amp;lt;token&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;X-API-Key: 123456
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;causes the browser to perform a Preflight Request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Content-Type&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If the request has a body, its &lt;code&gt;Content-Type&lt;/code&gt; must be one of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;application/x-www-form-urlencoded&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;multipart/form-data&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;text/plain&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using a content type such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;application/json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;makes the request non-simple and triggers a Preflight Request.&lt;/p&gt;

&lt;p&gt;This is one of the most common reasons developers see &lt;code&gt;OPTIONS&lt;/code&gt; requests in modern web applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This request is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/users&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No Preflight Request is sent.&lt;/p&gt;

&lt;p&gt;This request is also simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/login&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text/plain&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, no Preflight is required.&lt;/p&gt;

&lt;p&gt;However, this request is &lt;strong&gt;not&lt;/strong&gt; simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/login&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser first sends an &lt;code&gt;OPTIONS&lt;/code&gt; request.&lt;/p&gt;

&lt;p&gt;The same happens here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/profile&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Authorization&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bearer token&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because the &lt;code&gt;Authorization&lt;/code&gt; header is not CORS-safelisted, a Preflight Request is required.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A browser sends a Preflight Request only when a Cross-Origin request does not meet the requirements of a Simple Request.&lt;/p&gt;

&lt;p&gt;The most common triggers are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using methods such as &lt;code&gt;PUT&lt;/code&gt;, &lt;code&gt;PATCH&lt;/code&gt;, or &lt;code&gt;DELETE&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Including non-safelisted headers such as &lt;code&gt;Authorization&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Using content types like &lt;code&gt;application/json&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why seeing an &lt;code&gt;OPTIONS&lt;/code&gt; request before the actual request is completely normal in many modern APIs.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>security</category>
    </item>
    <item>
      <title>Understanding CORS Preflight Requests</title>
      <dc:creator>Alireza Hassankhani</dc:creator>
      <pubDate>Wed, 15 Jul 2026 11:32:55 +0000</pubDate>
      <link>https://dev.to/alireza_hassankhani_b8401/understanding-cors-preflight-requests-4bf</link>
      <guid>https://dev.to/alireza_hassankhani_b8401/understanding-cors-preflight-requests-4bf</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/alireza_hassankhani_b8401/understanding-the-http-options-method-4909"&gt;previous article&lt;/a&gt;, we introduced the HTTP &lt;code&gt;OPTIONS&lt;/code&gt; method and learned that it is commonly used to discover the capabilities of a resource.&lt;/p&gt;

&lt;p&gt;Now it's time to understand why browsers sometimes send an &lt;code&gt;OPTIONS&lt;/code&gt; request before the actual request.&lt;/p&gt;

&lt;p&gt;This process is called a &lt;strong&gt;Preflight Request&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Isn't CORS Enough?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As we've already learned, CORS is a browser security mechanism.&lt;/p&gt;

&lt;p&gt;If the server does not allow the requesting Origin, the browser prevents JavaScript from accessing the response.&lt;/p&gt;

&lt;p&gt;However, this raises an important question.&lt;/p&gt;

&lt;p&gt;What if the request itself could perform a sensitive operation on the server?&lt;/p&gt;

&lt;p&gt;For example, imagine a Cross-Origin request using the &lt;code&gt;DELETE&lt;/code&gt; or &lt;code&gt;PUT&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;If the browser always sent the request first and only blocked the response afterward, the server might already have deleted or modified data before the browser enforced CORS.&lt;/p&gt;

&lt;p&gt;To address this problem, browsers perform a &lt;strong&gt;Preflight Request&lt;/strong&gt; for certain Cross-Origin requests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How Does a Preflight Request Work?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before sending the actual request, the browser first sends an &lt;code&gt;OPTIONS&lt;/code&gt; request to the same endpoint.&lt;/p&gt;

&lt;p&gt;The browser is essentially asking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Am I allowed to send the upcoming request with these characteristics?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the server responds with the appropriate CORS headers, the browser proceeds with the actual request.&lt;/p&gt;

&lt;p&gt;Otherwise, the actual request is never sent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose the application intends to send:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="nf"&gt;DELETE&lt;/span&gt; &lt;span class="nn"&gt;/users/15&lt;/span&gt; &lt;span class="k"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt;
&lt;span class="na"&gt;Origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;https://app.example.com&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of sending it immediately, the browser first sends:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="nf"&gt;OPTIONS&lt;/span&gt; &lt;span class="nn"&gt;/users/15&lt;/span&gt; &lt;span class="k"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt;
&lt;span class="na"&gt;Origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;https://app.example.com&lt;/span&gt;
&lt;span class="na"&gt;Access-Control-Request-Method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;DELETE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This informs the server that a &lt;code&gt;DELETE&lt;/code&gt; request is about to be made.&lt;/p&gt;

&lt;p&gt;If the server approves it, the browser sends the actual request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server Response&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A typical response might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="k"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt; &lt;span class="m"&gt;204&lt;/span&gt; &lt;span class="ne"&gt;No Content&lt;/span&gt;
&lt;span class="na"&gt;Access-Control-Allow-Origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;https://app.example.com&lt;/span&gt;
&lt;span class="na"&gt;Access-Control-Allow-Methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;DELETE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the browser receives this response, it knows that sending the actual request is permitted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When Is a Preflight Request Sent?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Browsers do not send a Preflight Request for every Cross-Origin request.&lt;/p&gt;

&lt;p&gt;Preflight is only required when the request is &lt;strong&gt;not a Simple Request&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We'll define what qualifies as a Simple Request in the next article.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CORS vs. Preflight&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Although they're closely related, they serve different purposes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CORS&lt;/strong&gt; determines whether JavaScript is allowed to access the response.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Preflight&lt;/strong&gt; determines whether the browser is allowed to send the actual request in the first place.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In other words, Preflight happens &lt;strong&gt;before&lt;/strong&gt; the request, while CORS enforcement primarily affects &lt;strong&gt;access to the response&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the next article, we'll explore what makes a request "simple" and why only certain requests require a Preflight check.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>security</category>
    </item>
    <item>
      <title>Understanding the HTTP OPTIONS Method</title>
      <dc:creator>Alireza Hassankhani</dc:creator>
      <pubDate>Wed, 15 Jul 2026 10:55:34 +0000</pubDate>
      <link>https://dev.to/alireza_hassankhani_b8401/understanding-the-http-options-method-4909</link>
      <guid>https://dev.to/alireza_hassankhani_b8401/understanding-the-http-options-method-4909</guid>
      <description>&lt;p&gt;So far, we've explored concepts such as &lt;strong&gt;Origin&lt;/strong&gt;, &lt;strong&gt;CORS&lt;/strong&gt;, and &lt;strong&gt;Fetch Credentials&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the next article, we'll discuss &lt;strong&gt;Preflight Requests&lt;/strong&gt;, but before that, it's important to understand the HTTP &lt;code&gt;OPTIONS&lt;/code&gt; method, since browsers rely on it during the preflight process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is the OPTIONS Method?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;OPTIONS&lt;/code&gt; is one of the standard HTTP request methods.&lt;/p&gt;

&lt;p&gt;Unlike methods such as &lt;code&gt;GET&lt;/code&gt; or &lt;code&gt;POST&lt;/code&gt;, which retrieve or modify resources, the &lt;code&gt;OPTIONS&lt;/code&gt; method is used to discover the communication capabilities of a server for a particular resource.&lt;/p&gt;

&lt;p&gt;In simple terms, the client is asking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"If I want to interact with this resource, what operations do you support?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The server typically does not perform any business logic or modify data. Instead, it simply returns information about the resource's supported capabilities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How is OPTIONS Different from Other HTTP Methods?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Common HTTP methods are designed to perform specific actions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;GET&lt;/code&gt; — Retrieve data&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;POST&lt;/code&gt; — Create a resource&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PUT&lt;/code&gt; — Replace a resource&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PATCH&lt;/code&gt; — Partially update a resource&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DELETE&lt;/code&gt; — Remove a resource&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;OPTIONS&lt;/code&gt; method is different.&lt;/p&gt;

&lt;p&gt;Its purpose is not to manipulate resources but to describe what the server is willing to accept for a given endpoint.&lt;/p&gt;

&lt;p&gt;For this reason, &lt;code&gt;OPTIONS&lt;/code&gt; is generally considered an informational request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example Response&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose a client sends:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="nf"&gt;OPTIONS&lt;/span&gt; &lt;span class="nn"&gt;/users&lt;/span&gt; &lt;span class="k"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt;
&lt;span class="na"&gt;Host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;api.example.com&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server may respond with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="k"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt; &lt;span class="m"&gt;204&lt;/span&gt; &lt;span class="ne"&gt;No Content&lt;/span&gt;
&lt;span class="na"&gt;Allow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;GET, POST, PUT, DELETE, OPTIONS&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Allow&lt;/code&gt; header lists the HTTP methods supported by the resource.&lt;/p&gt;

&lt;p&gt;In many modern APIs, an OPTIONS response may also include additional headers that are especially important for CORS, which we'll cover in the next article.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do Developers Usually Send OPTIONS Requests?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In most applications, developers rarely send &lt;code&gt;OPTIONS&lt;/code&gt; requests manually.&lt;/p&gt;

&lt;p&gt;Instead, browsers automatically generate them in specific situations before sending the actual request.&lt;/p&gt;

&lt;p&gt;The browser does this to verify whether the upcoming request is permitted.&lt;/p&gt;

&lt;p&gt;This automatic verification process is known as a &lt;strong&gt;Preflight Request&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Should You Understand OPTIONS?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Many developers are surprised when they notice an &lt;code&gt;OPTIONS&lt;/code&gt; request in the browser's Network tab and assume their application generated it.&lt;/p&gt;

&lt;p&gt;In reality, it's usually the browser performing a protocol-level check before sending the actual request.&lt;/p&gt;

&lt;p&gt;Understanding the purpose of the &lt;code&gt;OPTIONS&lt;/code&gt; method makes it much easier to understand how &lt;strong&gt;CORS Preflight Requests&lt;/strong&gt; work.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>security</category>
      <category>programming</category>
    </item>
    <item>
      <title>Understanding the credentials Option in the Fetch API</title>
      <dc:creator>Alireza Hassankhani</dc:creator>
      <pubDate>Tue, 14 Jul 2026 11:27:09 +0000</pubDate>
      <link>https://dev.to/alireza_hassankhani_b8401/understanding-the-credentials-option-in-the-fetch-api-550</link>
      <guid>https://dev.to/alireza_hassankhani_b8401/understanding-the-credentials-option-in-the-fetch-api-550</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/alireza_hassankhani_b8401/cookies-and-cors-when-are-cookies-actually-sent-3cjl"&gt;previous article&lt;/a&gt;, we learned how &lt;code&gt;credentials: "include"&lt;/code&gt; allows the browser to include eligible Cookies in Cross-Origin requests.&lt;/p&gt;

&lt;p&gt;However, &lt;code&gt;include&lt;/code&gt; is only one of the available credential modes.&lt;/p&gt;

&lt;p&gt;In this article, we'll explore all three values of the &lt;code&gt;credentials&lt;/code&gt; option, explain their behavior, and see how they relate to Cookies and CORS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is the &lt;code&gt;credentials&lt;/code&gt; Option?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;credentials&lt;/code&gt; option tells the browser whether authentication-related information should be included with an HTTP request.&lt;/p&gt;

&lt;p&gt;This includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cookies&lt;/li&gt;
&lt;li&gt;HTTP Authentication credentials&lt;/li&gt;
&lt;li&gt;TLS Client Certificates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Fetch API supports three credential modes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;omit&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;same-origin&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;include&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each mode behaves differently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;credentials: "omit"&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;omit&lt;/code&gt; is used, the browser never includes credentials with the request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://api.example.com/profile&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;omit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this mode:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cookies are never sent.&lt;/li&gt;
&lt;li&gt;HTTP Authentication credentials are omitted.&lt;/li&gt;
&lt;li&gt;TLS Client Certificates are not included.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even if the user is authenticated, the request is treated as anonymous.&lt;/p&gt;

&lt;p&gt;This mode is typically used when requesting public resources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;credentials: "same-origin"&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the &lt;strong&gt;default behavior&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you don't explicitly specify the &lt;code&gt;credentials&lt;/code&gt; option, the browser automatically uses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;same-origin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this mode:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Credentials are included only for Same-Origin requests.&lt;/li&gt;
&lt;li&gt;Credentials are excluded from Cross-Origin requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, if a request is sent from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://app.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://app.example.com/api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cookies are included.&lt;/p&gt;

&lt;p&gt;However, if the request targets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://api.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cookies are not included because the request is Cross-Origin.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;credentials: "include"&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;include&lt;/code&gt; mode allows the browser to consider credentials even for Cross-Origin requests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://api.example.com/profile&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;include&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This does &lt;strong&gt;not&lt;/strong&gt; guarantee that Cookies will be sent.&lt;/p&gt;

&lt;p&gt;Instead, the browser evaluates the Cookie based on attributes such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Domain&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Path&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SameSite&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Secure&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Only Cookies that satisfy these rules are included in the request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Relationship with Cookies&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;credentials&lt;/code&gt; option directly affects whether Cookies are eligible to be included in a request.&lt;/p&gt;

&lt;p&gt;In summary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;omit&lt;/code&gt; → Never send Cookies.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;same-origin&lt;/code&gt; → Send Cookies only with Same-Origin requests.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;include&lt;/code&gt; → Allow eligible Cookies to be included in Cross-Origin requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's important to remember that &lt;code&gt;include&lt;/code&gt; only allows the browser to consider Cookies—it does not force them to be sent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Relationship with CORS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;include&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the server should also return:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;Access-Control-Allow-Credentials: true
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition, the &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt; header must contain the requesting Origin rather than a wildcard (&lt;code&gt;*&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;These headers determine whether JavaScript is allowed to access the response.&lt;/p&gt;

&lt;p&gt;They do &lt;strong&gt;not&lt;/strong&gt; necessarily determine whether the Cookie is sent.&lt;/p&gt;

&lt;p&gt;It's entirely possible for the browser to send a Cookie, for the server to process the request successfully, and yet for JavaScript to be denied access to the response because the CORS policy was not satisfied.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Fetch API provides three credential modes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;omit&lt;/code&gt; — Never send credentials.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;same-origin&lt;/code&gt; — Send credentials only with Same-Origin requests (default).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;include&lt;/code&gt; — Allow eligible credentials to be included in Cross-Origin requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding these modes is essential when working with authentication, Cookies, Sessions, and CORS in modern web applications.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>security</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>🍪 Cookies and CORS — When Are Cookies Actually Sent?</title>
      <dc:creator>Alireza Hassankhani</dc:creator>
      <pubDate>Mon, 13 Jul 2026 09:45:19 +0000</pubDate>
      <link>https://dev.to/alireza_hassankhani_b8401/cookies-and-cors-when-are-cookies-actually-sent-3cjl</link>
      <guid>https://dev.to/alireza_hassankhani_b8401/cookies-and-cors-when-are-cookies-actually-sent-3cjl</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/alireza_hassankhani_b8401/what-is-cors-and-why-does-it-exist-27em"&gt;previous article&lt;/a&gt;, we briefly discussed the relationship between &lt;strong&gt;Cookies&lt;/strong&gt; and &lt;strong&gt;CORS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this article, we'll take a closer look at how browsers decide whether a Cookie should be included in a Cross-Origin request.&lt;/p&gt;

&lt;p&gt;One of the most common misconceptions is that once CORS is configured correctly, Cookies are automatically sent with every request.&lt;/p&gt;

&lt;p&gt;In reality, that's not how browsers work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;📌 Default Browser Behavior&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When a Cross-Origin request is made using &lt;code&gt;fetch()&lt;/code&gt; or &lt;code&gt;XMLHttpRequest&lt;/code&gt;, browsers &lt;strong&gt;do not send Cookies, Authorization headers, or other credentials by default.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://api.example.com/profile&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even if the user is already logged into &lt;code&gt;api.example.com&lt;/code&gt;, the browser will &lt;strong&gt;not&lt;/strong&gt; include any Cookies with this request.&lt;/p&gt;

&lt;p&gt;This default behavior helps prevent authentication data from being unintentionally leaked across different Origins.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;📌 How Can We Send Cookies?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want the browser to include Cookies in a Cross-Origin request, you must explicitly use the &lt;code&gt;credentials&lt;/code&gt; option.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://api.example.com/profile&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;include&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;credentials: "include"&lt;/code&gt; does &lt;strong&gt;not&lt;/strong&gt; guarantee that Cookies will be sent.&lt;/p&gt;

&lt;p&gt;Instead, it tells the browser:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"If there are any Cookies that are eligible to be sent with this request, include them."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;📌 What Makes a Cookie Eligible?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Even with &lt;code&gt;credentials: "include"&lt;/code&gt;, the browser still evaluates the Cookie before sending it.&lt;/p&gt;

&lt;p&gt;Some of the most important checks include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Domain&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Path&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SameSite&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the Cookie's Domain doesn't match the request destination, it won't be sent.&lt;/li&gt;
&lt;li&gt;If the request path doesn't satisfy the Cookie's Path attribute, it won't be sent.&lt;/li&gt;
&lt;li&gt;If the Cookie's &lt;code&gt;SameSite&lt;/code&gt; policy blocks Cross-Site requests, it won't be sent.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In other words, &lt;code&gt;credentials&lt;/code&gt; is only the &lt;strong&gt;first requirement&lt;/strong&gt;, not the final decision.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;📌 Server Configuration Matters Too&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If your application expects JavaScript to access the response while using Cookies, the server must also be configured correctly.&lt;/p&gt;

&lt;p&gt;For example, the response should include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;Access-Control-Allow-Credentials: true
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition, the server should return an explicit Origin in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;Access-Control-Allow-Origin
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;instead of using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;*
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;because wildcard Origins cannot be used together with credentials.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;📌 An Important Distinction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sending a Cookie and allowing JavaScript to access the response are &lt;strong&gt;two different things&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It's entirely possible for this sequence to happen:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ The browser sends the request.&lt;/li&gt;
&lt;li&gt;✅ The Cookie is included in the request.&lt;/li&gt;
&lt;li&gt;✅ The server receives the Cookie and processes the request.&lt;/li&gt;
&lt;li&gt;✅ The server generates a valid response.&lt;/li&gt;
&lt;li&gt;❌ The browser blocks JavaScript from reading that response because the CORS policy is not satisfied.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, a CORS error does &lt;strong&gt;not&lt;/strong&gt; necessarily mean that the Cookie wasn't sent.&lt;/p&gt;

&lt;p&gt;It may simply mean that the browser refused to expose the response to JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;📌 Summary&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For a Cookie to be included in a Cross-Origin request, several conditions must be met:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The request must use &lt;code&gt;credentials: "include"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The Cookie itself must satisfy its own rules (&lt;code&gt;Domain&lt;/code&gt;, &lt;code&gt;Path&lt;/code&gt;, &lt;code&gt;SameSite&lt;/code&gt;, etc.).&lt;/li&gt;
&lt;li&gt;The server must return &lt;code&gt;Access-Control-Allow-Credentials: true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, even if the Cookie is successfully sent, an incorrect CORS configuration may still prevent JavaScript from accessing the response.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>What is CORS and Why Does It Exist?</title>
      <dc:creator>Alireza Hassankhani</dc:creator>
      <pubDate>Mon, 13 Jul 2026 09:40:31 +0000</pubDate>
      <link>https://dev.to/alireza_hassankhani_b8401/what-is-cors-and-why-does-it-exist-27em</link>
      <guid>https://dev.to/alireza_hassankhani_b8401/what-is-cors-and-why-does-it-exist-27em</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/alireza_hassankhani_b8401/the-difference-between-site-and-origin-2gld"&gt;previous article&lt;/a&gt;, we learned that an &lt;strong&gt;Origin&lt;/strong&gt; consists of three components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scheme (Protocol)&lt;/li&gt;
&lt;li&gt;Host&lt;/li&gt;
&lt;li&gt;Port&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Browsers use these three components to determine whether a request is &lt;strong&gt;Same-Origin&lt;/strong&gt; or &lt;strong&gt;Cross-Origin&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Whenever a web page attempts to access resources from a different Origin, a security mechanism called &lt;strong&gt;CORS (Cross-Origin Resource Sharing)&lt;/strong&gt; comes into play.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;📌 Why Does a CORS Error Occur?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose your web application is running at:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;https://app.example.com
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it tries to fetch data from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;https://api.example.com
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although both URLs belong to &lt;code&gt;example.com&lt;/code&gt;, their Hosts are different.&lt;/p&gt;

&lt;p&gt;That means they have different Origins.&lt;/p&gt;

&lt;p&gt;As a result, the browser treats this as a &lt;strong&gt;Cross-Origin&lt;/strong&gt; request.&lt;/p&gt;

&lt;p&gt;If the destination server does not explicitly allow this Origin, the browser prevents JavaScript from accessing the response, resulting in what we commonly call a &lt;strong&gt;CORS Error&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;Important:&lt;/strong&gt; CORS is a &lt;strong&gt;browser security mechanism&lt;/strong&gt;, not a server security mechanism.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;⚠️ A Common Misconception About CORS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Many developers believe that a CORS error means the request never reached the server.&lt;/p&gt;

&lt;p&gt;In most cases, that's simply not true.&lt;/p&gt;

&lt;p&gt;Typically:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ The browser sends the request.&lt;/li&gt;
&lt;li&gt;✅ The server receives it.&lt;/li&gt;
&lt;li&gt;✅ The server generates and returns a response.&lt;/li&gt;
&lt;li&gt;❌ The browser blocks JavaScript from accessing that response.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In other words, the request was successful—the browser simply refuses to expose the response to your application because the CORS policy was not satisfied.&lt;/p&gt;

&lt;p&gt;This is why sending the exact same request using tools like &lt;strong&gt;Postman&lt;/strong&gt; or &lt;strong&gt;curl&lt;/strong&gt; usually works without any problems.&lt;/p&gt;

&lt;p&gt;Those tools are not browsers, so they do not enforce browser security policies like CORS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;📦 How Does the Server Handle CORS?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To allow JavaScript to access the response, the server must include the appropriate CORS headers.&lt;/p&gt;

&lt;p&gt;The most important one is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;Access-Control-Allow-Origin: https://app.example.com
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This header tells the browser that JavaScript running on &lt;code&gt;https://app.example.com&lt;/code&gt; is allowed to read the response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✅ Examples&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose your Front-end is running at:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;https://app.example.com
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the server responds with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;Access-Control-Allow-Origin: https://app.example.com
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser allows JavaScript to access the response. ✅&lt;/p&gt;

&lt;p&gt;Now suppose the Front-end is running at:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;https://admin.example.com
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but the server still responds with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;Access-Control-Allow-Origin: https://app.example.com
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser blocks access to the response because the Origin does not match. ❌&lt;/p&gt;

&lt;p&gt;Another example:&lt;/p&gt;

&lt;p&gt;Front-end:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;https://evil.com
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Server response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;Access-Control-Allow-Origin: https://app.example.com
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, the browser blocks the response because &lt;code&gt;evil.com&lt;/code&gt; is not an allowed Origin.&lt;/p&gt;

&lt;p&gt;If the server instead responds with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;Access-Control-Allow-Origin: *
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then every Origin is allowed to access the resource.&lt;/p&gt;

&lt;p&gt;However, this should only be used for public resources. APIs that require authentication or expose sensitive data should never rely on a wildcard Origin.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;📌 CORS and Cookies&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When Cookies or other authentication credentials need to be included in a Cross-Origin request, configuring only &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt; is not enough.&lt;/p&gt;

&lt;p&gt;Additional settings are required, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Access-Control-Allow-Credentials&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;credentials&lt;/code&gt; option in the Fetch API&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll cover these in detail in the next article.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;📌 Summary&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Whenever a request is made to a different Origin, the browser applies the &lt;strong&gt;Same-Origin Policy&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If the server explicitly allows the requesting Origin using the appropriate CORS headers, JavaScript is allowed to access the response.&lt;/p&gt;

&lt;p&gt;Otherwise, the browser hides the response from JavaScript and reports a CORS error.&lt;/p&gt;

&lt;p&gt;This is why fixing CORS issues is usually a Back-end configuration task—not a Front-end one.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>security</category>
      <category>webdev</category>
    </item>
    <item>
      <title>the Difference Between Site and Origin</title>
      <dc:creator>Alireza Hassankhani</dc:creator>
      <pubDate>Mon, 13 Jul 2026 09:37:32 +0000</pubDate>
      <link>https://dev.to/alireza_hassankhani_b8401/the-difference-between-site-and-origin-2gld</link>
      <guid>https://dev.to/alireza_hassankhani_b8401/the-difference-between-site-and-origin-2gld</guid>
      <description>&lt;p&gt;&lt;strong&gt;🌐 Understanding the Difference Between Site and Origin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before diving into topics like &lt;strong&gt;CORS&lt;/strong&gt;, the &lt;strong&gt;Access-Control-Allow-Origin&lt;/strong&gt; header, the &lt;strong&gt;credentials&lt;/strong&gt; option in the Fetch API, or even Cookie behavior, it's important to understand two fundamental concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Site&lt;/li&gt;
&lt;li&gt;Origin&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Many of the errors developers encounter when communicating between the Front-end and Back-end are directly related to these two concepts.&lt;/p&gt;

&lt;p&gt;Once you understand the difference between them, browser behavior regarding Cross-Origin and Cross-Site requests becomes much easier to understand.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🌍 Let's Start with Domain and Subdomain&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every website is identified by a &lt;strong&gt;Domain&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a Domain.&lt;/p&gt;

&lt;p&gt;If you place another label before the Domain, it becomes a &lt;strong&gt;Subdomain&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;api.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;example.com&lt;/code&gt; → Domain&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;api&lt;/code&gt; → Subdomain&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are a few more examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mail.google.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;google.com&lt;/code&gt; → Domain&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mail&lt;/code&gt; → Subdomain
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;blog.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;example.com&lt;/code&gt; → Domain&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;blog&lt;/code&gt; → Subdomain
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;shop.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;example.com&lt;/code&gt; → Domain&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;shop&lt;/code&gt; → Subdomain&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each Subdomain can host an entirely different service, application, or API—even on a different server or port.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;📌 What is an Origin?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;Origin&lt;/strong&gt; consists of three components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scheme (Protocol)&lt;/li&gt;
&lt;li&gt;Host&lt;/li&gt;
&lt;li&gt;Port&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://api.example.com:443
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scheme → &lt;code&gt;https&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Host → &lt;code&gt;api.example.com&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Port → &lt;code&gt;443&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When determining whether two URLs share the same Origin, the browser compares all three components.&lt;/p&gt;

&lt;p&gt;If any one of them changes, the Origin changes.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are different Origins because the Scheme is different.&lt;/p&gt;

&lt;p&gt;Another example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://api.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are different Origins because the Host is different.&lt;/p&gt;

&lt;p&gt;And finally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://example.com:8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are different Origins because the Port is different.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🌐 What is a Site?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Unlike Origin, a &lt;strong&gt;Site&lt;/strong&gt; is less strict.&lt;/p&gt;

&lt;p&gt;A Site consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scheme (Protocol)&lt;/li&gt;
&lt;li&gt;Registrable Domain (eTLD+1)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Registrable Domain is the main domain that can actually be registered.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;api.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;blog.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both belong to the same Registrable Domain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Therefore, they are considered &lt;strong&gt;Same-Site&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;However:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;example.org
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;are &lt;strong&gt;Cross-Site&lt;/strong&gt; because the Registrable Domain is different.&lt;/p&gt;

&lt;p&gt;Also remember that the Scheme is part of the Site comparison.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;are considered different Sites in modern browsers (Schemeful Same-Site).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;⚖️ Site vs Origin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In summary:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Origin&lt;/strong&gt; compares:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scheme&lt;/li&gt;
&lt;li&gt;Host&lt;/li&gt;
&lt;li&gt;Port&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Site&lt;/strong&gt; compares:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scheme&lt;/li&gt;
&lt;li&gt;Registrable Domain&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because of this, two URLs can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Same-Site&lt;/li&gt;
&lt;li&gt;❌ Different-Origin&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://api.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://blog.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Same-Site&lt;/li&gt;
&lt;li&gt;❌ Different-Origin&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Another example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://example.com:8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Same-Site&lt;/li&gt;
&lt;li&gt;❌ Different-Origin&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;📌 Why Does This Matter?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Browsers rely on these concepts to enforce several important security mechanisms, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Same-Origin Policy (SOP)&lt;/li&gt;
&lt;li&gt;CORS&lt;/li&gt;
&lt;li&gt;Access-Control-Allow-Origin&lt;/li&gt;
&lt;li&gt;Fetch API &lt;code&gt;credentials&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Cookie &lt;code&gt;SameSite&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding the difference between &lt;strong&gt;Site&lt;/strong&gt; and &lt;strong&gt;Origin&lt;/strong&gt; makes it much easier to understand why browsers allow or block certain requests.&lt;/p&gt;

&lt;p&gt;In the next article, we'll explore &lt;strong&gt;Same-Origin Policy (SOP)&lt;/strong&gt; and &lt;strong&gt;CORS&lt;/strong&gt;, and see exactly how browsers decide whether a cross-origin request should be accessible to JavaScript.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Cookie attributes</title>
      <dc:creator>Alireza Hassankhani</dc:creator>
      <pubDate>Sun, 12 Jul 2026 20:34:50 +0000</pubDate>
      <link>https://dev.to/alireza_hassankhani_b8401/cookie-attributes-3dbg</link>
      <guid>https://dev.to/alireza_hassankhani_b8401/cookie-attributes-3dbg</guid>
      <description>&lt;p&gt;🍪&lt;/p&gt;

&lt;h3&gt;
  
  
  A Deeper Look at Cookie Attributes
&lt;/h3&gt;

&lt;p&gt;📦 Set-Cookie Header&lt;/p&gt;

&lt;p&gt;When a server wants to create or update a cookie, it uses the Set-Cookie header in the response.&lt;/p&gt;

&lt;p&gt;Example of this header:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;Set-Cookie: session_id=ab12cd34ef56; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=3600
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, one might think a cookie is just a Key=Value pair:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;session_id=ab12cd34ef56
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But in reality, that's only the core part of the cookie.&lt;/p&gt;

&lt;p&gt;What the browser actually receives is the Set-Cookie header, which—alongside the main value—includes a set of attributes. These attributes determine:&lt;/p&gt;

&lt;p&gt;· Which domain the cookie is valid for&lt;br&gt;
· Which paths it should be sent to&lt;br&gt;
· How long it remains valid&lt;br&gt;
· Whether it should only be sent over HTTPS&lt;br&gt;
· Whether it's accessible via JavaScript&lt;br&gt;
· Whether it should be sent in cross-site requests&lt;/p&gt;

&lt;p&gt;Let's examine each of these attributes individually.&lt;/p&gt;



&lt;p&gt;🔒 HttpOnly&lt;/p&gt;

&lt;p&gt;The HttpOnly attribute specifies that the cookie can only be accessed via the HTTP/HTTPS protocol.&lt;/p&gt;

&lt;p&gt;When this attribute is enabled:&lt;/p&gt;

&lt;p&gt;· JavaScript cannot access the cookie&lt;br&gt;
· The cookie cannot be read via document.cookie&lt;br&gt;
· The cookie is only sent by the browser when making requests to the server&lt;/p&gt;

&lt;p&gt;Why does HttpOnly exist?&lt;/p&gt;

&lt;p&gt;One of the most common web attacks is XSS (Cross-Site Scripting).&lt;/p&gt;

&lt;p&gt;In this attack, the attacker manages to inject and execute their own JavaScript code on the page. If the cookie does not have HttpOnly set, the attacker can read the user's session cookie and send it to their own server.&lt;/p&gt;

&lt;p&gt;Enabling HttpOnly means that even if malicious JavaScript is executed, the session cookie cannot be stolen.&lt;/p&gt;

&lt;p&gt;💡 HttpOnly reduces the risk of cookie theft in XSS attacks, but it is not a replacement for preventing the XSS vulnerability itself.&lt;/p&gt;



&lt;p&gt;🔐 Secure&lt;/p&gt;

&lt;p&gt;The Secure attribute specifies that the cookie should only be sent over a secure HTTPS connection.&lt;/p&gt;

&lt;p&gt;If the user is using HTTP, the browser will not send the cookie.&lt;/p&gt;

&lt;p&gt;Why does Secure exist?&lt;/p&gt;

&lt;p&gt;If a cookie is sent over an unencrypted connection (HTTP), an attacker positioned between the user and the server (Man-in-the-Middle) can eavesdrop on the traffic and view or steal the cookie.&lt;/p&gt;

&lt;p&gt;Enabling Secure ensures the cookie is only transmitted over an encrypted connection, drastically reducing the risk of it being stolen in transit.&lt;/p&gt;



&lt;p&gt;🌍 Domain&lt;/p&gt;

&lt;p&gt;The Domain attribute specifies which domain the cookie is valid for.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Domain=example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the cookie will be usable for the main domain and, typically, its subdomains as well.&lt;/p&gt;

&lt;p&gt;However, if Domain is set to another domain, the browser will not send the cookie for that domain.&lt;/p&gt;

&lt;p&gt;This attribute defines the scope of the cookie's validity.&lt;/p&gt;




&lt;p&gt;📂 Path&lt;/p&gt;

&lt;p&gt;The Path attribute determines which URL paths on the website the cookie should be sent to.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Path=/admin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the cookie will only be sent for requests that fall under the /admin path.&lt;/p&gt;

&lt;p&gt;This feature ensures the cookie is only used in parts of the site where it's actually needed.&lt;/p&gt;




&lt;p&gt;⏳ Expires&lt;/p&gt;

&lt;p&gt;The Expires attribute specifies the exact expiration date and time of the cookie.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Expires=Wed, 01 Jan 2027 00:00:00 GMT
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this date is reached, the browser will delete the cookie.&lt;/p&gt;




&lt;p&gt;⌛ Max-Age&lt;/p&gt;

&lt;p&gt;The Max-Age attribute specifies the lifetime of the cookie in seconds.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Max-Age=3600
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means the cookie will be deleted exactly one hour after it was created.&lt;/p&gt;

&lt;p&gt;Difference Between Max-Age and Expires&lt;/p&gt;

&lt;p&gt;Both are used to determine when the cookie should be deleted, but they have an important difference:&lt;/p&gt;

&lt;p&gt;· Expires stores a specific date and time.&lt;br&gt;
· Max-Age specifies the remaining lifetime from the moment the cookie is created.&lt;/p&gt;

&lt;p&gt;For this reason, Max-Age is generally considered more reliable, as it does not depend on the user's system clock.&lt;/p&gt;




&lt;p&gt;🛡️ SameSite&lt;/p&gt;

&lt;p&gt;The SameSite attribute specifies whether the cookie should be sent in requests originating from other sites.&lt;/p&gt;

&lt;p&gt;The main purpose of this attribute is to reduce the risk of CSRF (Cross-Site Request Forgery) attacks.&lt;/p&gt;

&lt;p&gt;Its common values are:&lt;/p&gt;

&lt;p&gt;· Strict → The cookie is only sent in same-site requests.&lt;br&gt;
· Lax → The cookie is sent in most normal requests, but restricts many cross-site requests.&lt;br&gt;
· None → The cookie is sent in all requests; in this case, using Secure is also mandatory.&lt;/p&gt;

&lt;p&gt;Proper use of SameSite can be one of the most important defense layers against CSRF attacks.&lt;/p&gt;




&lt;p&gt;📌 Summary&lt;/p&gt;

&lt;p&gt;Each cookie attribute is designed to solve a specific security or functionality issue:&lt;/p&gt;

&lt;p&gt;· HttpOnly → Reduces the risk of cookie theft in XSS attacks&lt;br&gt;
· Secure → Prevents the cookie from being sent over insecure connections and reduces Man-in-the-Middle risks&lt;br&gt;
· SameSite → Reduces the likelihood of successful CSRF attacks&lt;br&gt;
· Domain → Defines which domains are allowed for the cookie&lt;br&gt;
· Path → Defines which URL paths are allowed to receive the cookie&lt;br&gt;
· Expires → Defines the expiration date&lt;br&gt;
· Max-Age → Defines the cookie's lifetime in seconds&lt;/p&gt;

&lt;p&gt;Understanding these attributes is one of the most important prerequisites for learning web security, because many common vulnerabilities are directly related to misconfiguring these very options.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>frontend</category>
      <category>backend</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
