<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="178515" data-post-id="178515">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="santoshbt" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/santoshbt/120/29908_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  santoshbt
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I a in a similar situation as this post owner. I have an API only app which has to produce the csrf token by get_csrf_token() and pass it on to angular app. Currently I am trying ot make a post request via Postman. I have set this token generated in header X-CSRF-Token or passing through parameter like _csrf_token. But always getting Invalid token InvalidCSRFTokenError.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="178515" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/csrf-and-postman/12910/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-178515" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="178515"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="178633" data-post-id="178633">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="voger" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/voger/120/3322_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  voger
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>For some reason, when you go through the API route, the <code>_csrf_token</code> key is missing from the session or set to the masked value. I don’t remember since has been a while since I had this problem. The way I made it work is create an endpoint where the app can get a new CSRF token and that endpoint sets the <code>_csrf_token</code> key in the session to the unmasked value</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def show(conn, _) do
    csrf_token = Plug.CSRFProtection.get_csrf_token()

    conn
    |&gt; fetch_session()
    |&gt; put_session("_csrf_token", Process.get(:plug_unmasked_csrf_token))
    |&gt; render("show.json", token: csrf_token)
  end
</code></pre>
<p>The idea is that each time you create a new CSRF token, you put the masked value in the session. I hope this helps.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="178633" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/csrf-and-postman/12910/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-178633" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="178633"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="178644" data-post-id="178644">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="santoshbt" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/santoshbt/120/29908_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  santoshbt
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks for your help.</p>
<p>I have put   plug :protect_from_forgery in routes<br>
and in some controller</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def csrf_token(conn, _params) do
    csrf_token = Plug.CSRFProtection.get_csrf_token
    conn
    |&gt; fetch_session()
    |&gt; put_session("_csrf_token", Process.get(:plug_unmasked_csrf_token))
    |&gt; put_view(MyAppWeb.APIV2.UserView)
    |&gt; render("session_token.json", token: csrf_token)
  end
</code></pre>
<p>For the first service request, it recognized the csrf token and worked properly.<br>
But subsequent request it is getting the same Invalid CSRF token error.<br>
Even I generated the new token, but not able to succeed.</p>
<p>Also please let me know, when this token will get cleared off, if it works in subsequent requests?</p>
<p>Please help on this.<br>
TIA..</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="178644" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/csrf-and-postman/12910/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-178644" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="178644"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="178727" data-post-id="178727">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="voger" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/voger/120/3322_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  voger
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I just tested it and it works as it should. I am not using Postman. I use Insomnia or RESTer for Firefox.</p>
<p>What I do is contact the token generating endpoint e.g.: <a href="http://localhost:4000/api/token" rel="noopener nofollow ugc">http://localhost:4000/api/token</a> which returns a json like</p>
<pre data-code-wrap="json"><code class="lang-json">{
  "token": "VxIYVQMwHXIAAFcGISBcEBsRchA-AmAhcYZgkqp4mr6nNM2hhdBcxTUl"
}
</code></pre>
<p>I put that token in every request I want to make to the server in the <code>x-csrf-token</code> header and it works fine. If I contact again that endpoint it will return a new token and I will have to update the header in all the requests because the old token is now invalid.</p>
<p>Maybe there is some problem with the cookies, session or maybe the app contacts often the token endpoint and doesn’t update the requests header.</p>
<p>I would suggest you inspect the <code>conn</code> struct in the failing controller, once when it succeeds and once when it fails. Look under the <code>req_headers</code> key if there is any <code>"x-csrf-token"</code> tuple and if it’s value is the same both times.</p>
<p>Also check under the <code>:private</code> key for   <code>:plug_session =&gt; %{"_csrf_token" =&gt; "lfiUShv-j1sI5Q4z20k7B-WD"}</code> and if the <code>_csrf_token</code> value remains the same.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="178727" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/csrf-and-postman/12910/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-178727" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="178727"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="178741" data-post-id="178741">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="santoshbt" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/santoshbt/120/29908_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  santoshbt
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I found the major difference in  :private key for :plug_session.<br>
For succeeding request it is :plug_session =&gt; %{“_csrf_token” =&gt; “dYmLXL3R1plGyrLaFjyriM2U”},<br>
For failing request is is  :plug_session =&gt; %{},</p>
<p>Also with Postman, atleast we are able to get the First response working.<br>
With RESTer for chrome, none of the requests are recognizing the csrf token.</p>
<p>Thanks</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="178741" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/csrf-and-postman/12910/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-178741" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="178741"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>