2

Got a really bizarre problem with Invoke-RestMethod. I'm using it send a GET and include a cookie in the request:

$getEndpoint = "http://YYYYYYYYYYYYYY/clients/XXXXXX/dev"
$authheader = "auth_tkt=\""XXX"""
Invoke-RestMethod -Headers @{"cookie" = "$authheader"} -Uri $getEndpoint 

If I look at the request in Fiddler then I see this:

GET http://YYYYYYYYYYYYYY/clients/XXXXXX/dev HTTP/1.1 User-Agent: Mozilla/5.0 (Windows NT; Windows NT 6.1; en-GB) WindowsPowerShell/4.0 Host: YYYYYYYYYYYYYY Connection: Keep-Alive

Where's the cookie gone? Someonewhere along the pipe the cookie is disappearing. Any ideas why? I'm assuming I'm misunderstanding something somewhere.

In case it matters, I have attempted a similar request from curl and it works without issue (i.e. it authenticates using the provided cookie and I get the response I'm expecting):

curl -H "cookie: auth_tkt=\"XXX" http://YYYYYYYYYYYYYY/clients/XXXXX/dev

jamiet
  • 10,501
  • 14
  • 80
  • 159
  • No, double quotes are a red herring I'm afraid. This had the same problem: `$getEndpoint = "http://YYYYYYYYYYYYYY/clients/XXXXXX/dev" $authheader = "some useless string" Invoke-RestMethod -Headers @{"cookie" = "$authheader"} -Uri $getEndpoint ` – jamiet Mar 05 '15 at 09:14
  • Awesome, thank you @Kayasax, webclient worked perfectly. If you post this as answer then I'll mark it as such. – jamiet Mar 05 '15 at 10:26

1 Answers1

5

The invoke-restmethod's doc says you can't pass cookie through -headers :

Headers Specifies the headers of the web request. Enter a hash table or dictionary. To set UserAgent headers, use the UserAgent parameter. You cannot use this parameter to specify UserAgent or cookie headers

An alternative could be to use webclient and a cookieContainer; have a look at that post : powershell httpwebrequest GET method cookiecontainer problem?

Community
  • 1
  • 1
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103