1

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.

3
  • Instead of headers, use -ContentType 'application\json'. Are your domains valid? Commented Nov 1, 2017 at 19:24
  • Changing the content type didn't seem to help. I'm using the domain of the web site that I am trying to access. Commented Nov 1, 2017 at 19:35
  • are you getting an error message or just a null return? if your getting a weird object back Invoke-RestMethod might be worth a try Commented Nov 1, 2017 at 20:00

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the suggestion, I tried that and no-go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.