Reputation: 330
I am trying to make an API request in Powershell.
The curl command that works:
curl -k -v -X GET -H "Cookie: customer=<valueA>;JSESSIONID=<ValueB>" -H "Accept: application/json" https://someurl.net/path/path
I Tried:
$session = New-Object Microsoft.Powershell.Commands.WebRequestSession
$cookie = New-Object System.Net.Cookie
$cookie2 = New-Object System.Net.Cookie
$cookie.Name = "customer"
$cookie.Value = "<valueA>"
$cookie.Domain = "https://someurl.net"
$cookie2.Name = "JSESSIONID"
$cookie2.Value = "<valueB>"
$cookie2.Domain = "https://someurl.net"
$session.Cookies.Add($cookie, $cookie2);
Invoke-WebRequest -Uri "https://someurl.net/path/path" -Method Get -WebSession $session -Headers @{"accept"="application/json"}
This should return a json payload. Any help is appreciated.
Upvotes: 1
Views: 3128
Reputation: 11
Here might be the problem:
$session.Cookies.Add($cookie, $cookie2);
According to definition:
PS C:\> $session.Cookies.Add
OverloadDefinitions
-------------------
void Add(System.Net.Cookie cookie)
void Add(System.Net.CookieCollection cookies)
void Add(uri uri, System.Net.Cookie cookie)
void Add(uri uri, System.Net.CookieCollection cookies)
You should change it to:
$session.Cookies.Add($cookie);
$session.Cookies.Add($cookie2);
Upvotes: 1