jamiet
jamiet

Reputation: 12344

Invoke-RestMethod isn't passing headers

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

Upvotes: 3

Views: 2528

Answers (1)

Loïc MICHEL
Loïc MICHEL

Reputation: 26170

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?

Upvotes: 5

Related Questions