Thursday, December 14, 2017

Keeping cookies in PowerShell web requests

PowerShell's Invoke-WebRequest cmdlet can be used for all kinds of HTTP requests. For stateful web applications, it might be necessary to retain and retransmit cookies in subsequent requests. Conveniently, the cmdlet has features for maintaining a session.

The first request to the site should specify a variable (by name) into which the session should be saved:

iwr https://example.com/Page1 -SessionVariable session

Then all later requests supply that object as the session:

iwr https://example.com/Page2 -WebSession $session

Based on my Super User answer.

No comments:

Post a Comment