title: Guzzle add CSRF token
body: <p>I am trying to create a product in drupal commerce using guzzle and restful webservices.</p>
  
  <p>When i am trying to create a new product i use the following process:
   1) Login as a user who has the rights to create a product (succes)
   2) Request CSRF token (succes)
   3) Create product : Uh oh!: Client error response [status code] 401 [reason phrase] Unauthorized: Access to this operation not granted </p>
  
  <p>Now when i am doing this process trough poster (Firfox add-on). I can succesfully create a product, but it guzzle i always fail to acces this operation.</p>
  
  <pre><code>require 'guzzle.phar';
  use Guzzle\Http\Client;
  //EDIT:
  use Guzzle\Plugin\Cookie\CookiePlugin;
  use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;
  
  $client = new Client('http://localhost:8888/drupal-test/');
  
  //EDIT:
  $cookiePlugin = new CookiePlugin(new FileCookieJar($cookie_file_name));
  $client-&gt;addSubscriber($cookiePlugin);
  
  //User login
  $request = $client-&gt;post('my_services/user/login.json', null,
                      array(
                              'username' =&gt; 'its',
                              'password' =&gt; '****'
                      ));
  
  $response_json = $response-&gt;getBody();
  $session = $response_var-&gt;session_name . '=' . $response_var-&gt;sessid;
  
  //Services CSRF token
  $request = $client-&gt;get('services/session/token');
  $response = $request-&gt;send();
  $token = $response-&gt;getBody(TRUE);
  
  
  //Create Product
  $request = $client-&gt;post('my_services/product.json',
          array(
              'X-CSRF-Token' =&gt; $token,
           ),
          array(
              'sku' =&gt; 'guzzleproductadd10',
              'title' =&gt; 'guzzleproductadd10',
              'type' =&gt; 'product',
              'commerce_price_amount' =&gt; '200',
              'commerce_price_currency_code' =&gt; 'EUR'
          )
   );
  //$request-&gt;addHeader('X-CSRF-Token', $token);
  try {
       $response = $request-&gt;send();
  } 
  catch (Guzzle\Http\Exception\BadResponseException $e) {
      echo 'Uh oh!: ' . $e-&gt;getMessage();
  }
  </code></pre>
  
  <p>I don't really see what i am doing wrong. I logged in, added the required variables to the header. </p>
  
  <p>Ty!</p>
  
  <p>Edit: solution see comments. I adjusted  the code.</p>