title: How do I generate a CSRF token from the Rails console?
body: <p>I'm trying to make an authenticated post request, and I need the CSRF. When I log in, it isn't generating the <code>_csrf_token</code> for some reason:</p>
  
  <pre><code>2.0.0p247 :126 &gt;   app.post '/community_members/login', {"refinery_user[login]"=&gt;'chloe', 'refinery_user[password]'=&gt;'test'}
   =&gt; 302
  2.0.0p247 :127 &gt; app.session
   =&gt; {"warden.user.refinery_user.key"=&gt;[[56], "$2a$10$gr/rTcQfuXnes1Zml3qOPu"], "session_id"=&gt;"f77d89cef9ff1710890f575b479bb690"}
  </code></pre>
  
  <p>I tried <code>app.session[:_csrf_token] ||= SecureRandom.base64(32)</code> before login, but it is always deleted. I also tried to get the login form first, but <code>_csrf_token</code> is still not set.</p>
  
  <pre><code>2.0.0p247 :133 &gt;   app.get '/community_members/sign_in'
  2.0.0p247 :134 &gt; app.response # authenticity_token is burried in the raw HTML
  2.0.0p247 :136 &gt;   app.post '/community_members/login', {"refinery_user[login]"=&gt;'chloe', 'refinery_user[password]'=&gt;'test'}
  2.0.0p247 :137 &gt; app.session
   =&gt; {"warden.user.refinery_user.key"=&gt;[[56], "$2a$10$gr/rTcQfuXnes1Zml3qOPu"], "session_id"=&gt;"c2c564229e55b81ca788788558d7d11a"}
  </code></pre>
  
  <p>How do I manually generate the token to pass to the post request?</p>
  
  <hr>
  
  <p>Oh ok, I think I need to submit the authenticity_token (that is set in the session after GETing the login form) to the login form, then it puts it in the session permanently! This worked:</p>
  
  <blockquote>
    <p>app.post '/community_members/login', {'authenticity_token'=>'GfT5GtcUmYQ927oNQmh2MR0NKQucGSx8mtMg3Ph9kXw=', "refinery_user[login]"=>'chloe', 'refinery_user[password]'=>'test'}</p>
  </blockquote>
  
  <p>Here is a full example: <a href="https://stackoverflow.com/a/23899701/148844">https://stackoverflow.com/a/23899701/148844</a></p>