title: Bash script Curl get cookie and re-use? (Error 400. request badly formed)
body: <p>I want to get Cookie from 1 URL and use this Cookie for next Curl call.</p>
  
  <p>With the code below I'm able to get the Cookie as <code>$session</code>. But the 2. Curl call fails.</p>
  
  <p>Here is what I tried:</p>
  
  <pre><code>#!/bin/bash
  session=$(curl -sD - 'http://www.example.com/getLogInCookie'  -H 'Cookie: session.LogIn=True;' -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' -H 'Referer: http://www.example.com/login' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.9,nb;q=0.8' --compressed --insecure | tail -n+8 | head -1 | sed "s/Set-Cookie: session.LogInHash=//g" | sed "s/; path=\/; HttpOnly//g");
  
  #session='lwiqadgdlykt'; # If uncomment this line then all work as expected.
  echo $session;
  
  cookie="Cookie: session.LogIn=True; session.LogInHash=$session";
  result=$(curl -v 'http://www.example.com/StartSession' -H "$cookie" -H 'Connection: keep-alive' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'X-Requested-With: XMLHttpRequest' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36' -H 'Referer: http://www.example.com/login' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.9,nb;q=0.8' --compressed --insecure);
  
  echo $result;    # Here result fail with: Error 400. The request is badly formed.
  </code></pre>
  
  <p>I see I get the Cookie in <code>$session</code> but its not passed to the next Curl call.</p>
  
  <p>If I set <code>$session</code> with the Cookie I got in first Curl call like: <code>session='lwiqadgdlykt';</code> and retry then I see the session is working.</p>
  
  <p>How can I send/pass the Cookie from 1. Curl to 2. Curl?</p>