2017-01-12 14 views
0

こんにちは私はPowershell v4で次の 'Invoke-RestMethed'コマンドを実行していますが、HTTP 406エラーが発生しています。Powershell v4 Invoke-RestMethod:HTTP Status 406

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

私の理解では、サーバがXML形式で要求をとるが、JSON形式で返すと、多分問題を引き起こしてthatsのです:

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

は、私は、ヘッダーに次の変更を行いましたか? "Accept" = "application/json"にヘッダを変更しようとしましたが、同じエラーが発生しました。

全エラー:

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.

+0

あなたはそれに対して働いているかURL知らずして、任意の提案を与えることは困難です問題はおそらく私の経験ではそのURLに固有です。 – TravisEz13

+0

VMware NSXマネージャの内部URL '$ Url =" https://nsxmgr-l-01a.corp.local/api/1.0/nsx/cli?action = execute "' – user3784080

+0

Acceptヘッダーを削除しようとしましたか? PowerShellを使用してNSX APIを呼び出す他のサンプルを見ると、Authorizationヘッダーのみが使用されます。 – TravisEz13

答えて

0

この問題のためのStackOverflowの美しい機能があります。ここにリンクがある:Execute-Request

これはあなたを助ける必要があります。

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 
} 

用途:

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

Execute-Request -Url "https://URL/ticket" -Credentials $credentials -Header @{"Accept" = "application/json"} -ContentType "application/json" 
+0

ありがとう@ Ranadip残念ながら、私はこの機能に' -Body'を追加できないようです。 – user3784080