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

Web ServicesPowershell

Web Services Problem Overview


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?

Web Services Solutions


Solution 1 - Web Services

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

Introduced in POSH 3.0

Solution 2 - Web Services

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 - Web Services

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

Solution 4 - Web Services

One more easy way to send Authorization and perform Get or Delete request in Loop

$orders=@("522536","522539")

foreach ($order in $orders){
    iwr "https://api.taxjar.com/v2/transactions/orders/$order" `
      -Method 'DELETE'`
      -Headers @{ 'Authorization' ='Token token="34625gdsgd74rghdg4"'}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoeyView Question on Stackoverflow
Solution 1 - Web ServicesMadu AlikorView Answer on Stackoverflow
Solution 2 - Web ServicesWhat Would Be CoolView Answer on Stackoverflow
Solution 3 - Web ServicesBrianView Answer on Stackoverflow
Solution 4 - Web ServicesAidar GatinView Answer on Stackoverflow