0

Hi I'm running the following 'Invoke-RestMethed' command in Powershell v4 but it's throwing HTTP 406 error.

Invoke-RestMethod -Method Post -Uri $url -Headers $head -ContentType "application/xml" -Body $body -OutFile output.txt

I made following change to the header:

$head = @{"Authorization"="Basic $auth"; "Accept"="*/*"}

My understanding is the server takes request in xml format but return in JSON format and maybe thats causing the issue? I did tried changing header to "Accept"="application/json" but getting the same error.

Full error:

Invoke-RestMethod : HTTP Status 406 - type Status report message description The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.

TravisEz13
  • 2,263
  • 1
  • 20
  • 28
user3784080
  • 97
  • 2
  • 13
  • Without knowing what URL you are working against it is difficult to give any suggestion as the issue is probably specific to that URL in my experience. – TravisEz13 Jan 12 '17 at 23:08
  • Its an internal url to VMware NSX Manager `$Url = "https://nsxmgr-l-01a.corp.local/api/1.0/nsx/cli?action=execute"` – user3784080 Jan 12 '17 at 23:14
  • Have you tried removing the Accept header? Looking at other samples that call NSX APIs using PowerShell they only use the Authorization header. – TravisEz13 Jan 12 '17 at 23:38
  • Yes originally I made the api call without "Accept", just "Authorization" in header but I received this error so I added "Accept", but still getting the same error! Also most NSX api call don't have body with xml code in it and they are working fine but, some call required xml code, which fails! – user3784080 Jan 12 '17 at 23:48
  • [link] (http://stackoverflow.com/questions/18278977/powershell-v3-invoke-restmethod-headers) suggests `Since Accept header could not be specified for neither Invoke-RestMethod nor Invoke-WebRequest in PowerShell V3` This is not the case for PowerShell v4 right? – user3784080 Jan 12 '17 at 23:50
  • I know the "accept" header works in PowerShell V5 for the `Invoke-WebRequest` function. If you use `Invoke-WebRequest`, you definitely need to specify the Accept header. **Update:** verified accept header works for `Invoke-RestMethod` in PowerShell V5.1 – TravisEz13 Jan 12 '17 at 23:54

1 Answers1

0

There is beautiful function in StackOverflow for this issue. Here is the link : Execute-Request

This should help you out:

Function Execute-Request()
{
Param(
  [Parameter(Mandatory=$True)]
  [string]$Url,
  [Parameter(Mandatory=$False)]
  [System.Net.ICredentials]$Credentials,
  [Parameter(Mandatory=$False)]
  [bool]$UseDefaultCredentials = $True,
  [Parameter(Mandatory=$False)]
  [Microsoft.PowerShell.Commands.WebRequestMethod]$Method = [Microsoft.PowerShell.Commands.WebRequestMethod]::Get,
  [Parameter(Mandatory=$False)]
  [Hashtable]$Header,  
  [Parameter(Mandatory=$False)]
  [string]$ContentType  
)

   $client = New-Object System.Net.WebClient
   if($Credentials) {
     $client.Credentials = $Credentials
   }
   elseif($UseDefaultCredentials){
     $client.Credentials = [System.Net.CredentialCache]::DefaultCredentials 
   }
   if($ContentType) {
      $client.Headers.Add("Content-Type", $ContentType)
   }
   if($Header) {
       $Header.Keys | % { $client.Headers.Add($_, $Header.Item($_)) }  
   }     
   $data = $client.DownloadString($Url)
   $client.Dispose()
   return $data 
}

Usage:

Execute-Request -Url "https://URL/ticket" -UseDefaultCredentials $true

Execute-Request -Url "https://URL/ticket" -Credentials $credentials -Header @{"Accept" = "application/json"} -ContentType "application/json"
Community
  • 1
  • 1
Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45