title: JMeter Alter HTTP Headers During Test
body: <p>I'm attempting to test an HTTP service with JMeter.  The HTTP service requires authentication through a simple bearer token mechanism.  I'm trying to proceed as follows:</p>
  
  <ol>
  <li>POST authentication request</li>
  <li>Store token as a variable</li>
  <li>Set Authorization HTTP header to stored variable</li>
  <li>Request protected resource(s)</li>
  <li>Evaluate performance</li>
  </ol>
  
  <p>So far I've been able to post the request, get the token, extract it with regex, save it to a variable, and assert that the variable is appropriately set.</p>
  
  <p>The problem is getting the variable into the HTTP header.  In the "Header Manager" the value is set like this:</p>
  
  <p><img src="https://i.stack.imgur.com/lhdwp.png" alt="Header Manager Attempt"></p>
  
  <p>Unfortunately when the next requests are issued their authorization header has the value "Bearer ".  Searching around the internet led me to believe that headers are <a href="http://jmeter.512774.n5.nabble.com/HTTP-Header-Manager-uses-old-variable-values-td527582.html" rel="noreferrer">configured before the thread starts</a>, which would explain the "Bearer "</p>
  
  <p>My sampling/grouping/nesting is as follows:</p>
  
  <p><img src="https://i.stack.imgur.com/mAuya.png" alt="JMeter Nesting"></p>
  
  <p>All the tests pass up to get restricted resource, which fails with a 400, since the authorization header is malformed.</p>
  
  <p>I feel like I'm missing something really obvious, and/or approaching this problem the wrong way.</p>