title: Powershell use cookie to authenticate
body: <p>I'm trying to translate these two (working) curl commands, into powershell. 
  How do you save a cookie with Invoke-WebRequest?</p>
  
  <p>Get Session Cookie bash</p>
  
  <pre><code>curl -k \ 
  --cookie-jar cookie \
  -H 'Content-Type: application/json' \
  -d '{"jsonrpc": "2.0", "id": 1, "method": "login", "params": {"username": "bob", "password": "correct-horse-battery-staple"}}' \
  https://foo.example.com/api/session
  </code></pre>
  
  <p>Get Session Cookie powershell</p>
  
  <pre><code>$data = @{}
  $data.jsonrpc = '2.0'
  $data.id = '1'
  $data.method = 'login'
  $data.params = @{}
  $data.params.username = 'bob'
  $data.params.password = 'correct-horse-battery-staple'  
  $url = "https://foo.example.com/api/session"
  $webrequest = Invoke-WebRequest -Method POST `
  -ContentType 'application/json' `
  -Body $data `
  -Uri $url `
  -SessionVariable websession `
  -UseBasicParsing
  $cookies = $websession.Cookies.GetCookies($url)
  Write-Host "$($cookies[0].name) = $($cookies[0].value)"
  </code></pre>
  
  <p>Get version bash</p>
  
  <pre><code>curl -k \
  --cookie cookie \
  -H 'Content-Type: application/json' \
  -d '{"jsonrpc": "2.0", "id": 2, "method": "version"}' \
  https://foo.example.com/api/about
  </code></pre>
  
  <p>Get version powershell</p>
  
  <pre><code>$data = @{}
  $data.jsonrpc = '2.0'
  $data.id = '2'
  $data.method = 'version'
  $url = "https://foo.example.com/api/about"
  Invoke-WebRequest -Method POST `
  -ContentType 'application/json' `
  -Body $data `
  -Uri $url `
  -WebSession $websession `
  -UseBasicParsing
  </code></pre>
  
  <p>The second command is not passing in the cookie properly and returns error 'Decoding failed'</p>
  
  <pre><code>StatusCode        : 200
  StatusDescription : OK
  Content           : {"error":{"code":-32000,"message":"Decoding failed: Syntax error","data":null},"id":null}
  RawContent        : HTTP/1.1 200 OK
                      Pragma: no-cache
                      Keep-Alive: timeout=5, max=100
                      Connection: Keep-Alive
                      Content-Length: 89
                      Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
                      Content-Ty...
  Forms             :
  Headers           : {[Pragma, no-cache], [Keep-Alive, timeout=5, max=100], [Connection, Keep-Alive], [Content-Length, 89]...}
  Images            : {}
  InputFields       : {}
  Links             : {}
  ParsedHtml        :
  RawContentLength  : 89
  </code></pre>
  
  <p>Why does curl successfully authenticate with a cookie, but powershell errors? </p>
  
  <p>Looking at the variable $websession</p>
  
  <pre><code>$websession
  
  Headers               : {}
  Cookies               : System.Net.CookieContainer
  UseDefaultCredentials : False
  Credentials           :
  Certificates          :
  UserAgent             : Mozilla/5.0 (Windows NT; Windows NT 6.3; en-US)
                          WindowsPowerShell/5.0.10586.117
  Proxy                 :
  MaximumRedirection    : -1
  </code></pre>