powershellwebrequest

How to use Invoke-WebRequest to see the sent headers?


When I'm using the following command:

Invoke-WebRequest -UseBasicParsing -Uri http://example.com -SessionVariable Foo -UserAgent Bar

I get the following output:

StatusCode        : 200
StatusDescription : OK
Content           : <!doctype html>
                    <html>
                    <head>
                        <title>Example Domain</title>

                        <meta charset="utf-8" />
                        <meta http-equiv="Content-type" content="text/html; 
                    charset=utf-8" />
                        <meta name="viewport" conten...
RawContent        : HTTP/1.1 200 OK
                    Vary: Accept-Encoding
                    X-Cache: HIT
                    Content-Length: 1270
                    Cache-Control: max-age=604800
                    Content-Type: text/html
                    Date: Mon, 19 Feb 2018 16:38:28 GMT
                    Expires: Mon, 26 Feb 2018 16:38...
Forms             : 
Headers           : {[Vary, Accept-Encoding], [X-Cache, HIT], [Content-Length, 
                    1270], [Cache-Control, max-age=604800]...}
Images            : {}
InputFields       : {}
Links             : {@{outerHTML=<a 
                    href="http://www.iana.org/domains/example">More 
                    information...</a>; tagName=A; 
                    href=http://www.iana.org/domains/example}}
ParsedHtml        : 
RawContentLength  : 1270

But the problem is, that I cannot see the headers which were sent (either the session variable or the user agent). Maybe it's get truncated by ...?

How can I display the headers which were sent?


Solution

  • Invoke-WebRequest doesn't store the request header AFAIK. If you want specific values in the header then it expects you to specify them with the parameters -Headers and -UserAgent.

    HttpWebRequest or WebRequest provides more control if you prefer that.

    $req = [System.Net.HttpWebRequest]::Create("http://www.stackoverflow.com")
    $res = $req.GetResponse()
    
    $req.Headers.Keys | % { "$_ = $($req.Headers[$_])" }
    Host = stackoverflow.com
    
    $req | fl Method, UserAgent, ProtocolVersion
    Method          : GET
    UserAgent       :
    ProtocolVersion : 1.1