Is it possible to send additional HTTP headers to web services via New-WebServiceProxy

A web service I need to interact with (often manually for testing) requires an extra HTTP header on certain requests. Quick manual testing works quite great with PowerShell's New-WebServiceProxy but so far I haven't found an option to add another HTTP header to the request.

Is there something for that?


Solution 1:

Invoke-WebRequest http://yourURLhere -Headers @{"accept"="application/json"}

Introduced in POSH 3.0

Solution 2:

You could use .NET's web client from PowerShell.

> $webClient = New-Object System.Net.WebClient
> $webClient.Headers.add('accept','application/json')
> $webClient.DownloadString('http://yourURLhere')

Solution 3:

I am surprised this hasn't come up:

$uri = 'http://...'

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Accept','Application/Json')
$headers.Add('X-My-Header','...')

$result = Invoke-WebRequest -Uri $uri -Headers $headers

For completeness, a reference to the headers property:

https://msdn.microsoft.com/en-us/library/s4ys34ea(v=vs.110).aspx