7

ドキュメントで次のように指定されているサービスへの RestAPI 呼び出しを実行しようとしています。

Integration Server は、XML および JSON 形式で応答できます。リクエストでは、次の Accept ヘッダーのいずれかを使用します。

  1. 受け入れる: application/json, / .
  2. 受け入れる: アプリケーション/xml、/

accept ヘッダーに application/xml、application/json、または/が含まれていない場合、統合サーバーは「406 メソッドは受け入れられません」というステータス コードで応答します。

私のpowershellコードは次のようになります Invoke-RestMethod -URI https://URL/ticket -Credential $cred -Method Get -Headers @{"Accept"="application/xml"}

しかし、ヘッダーに関連する次のエラーが発生します。 Invoke-RestMethod : This header must be modified using the appropriate property or method. Parameter name: name

powershell で Accept ヘッダーを指定できない理由を理解するのを手伝ってくれる人はいますか? または、ここで見逃している別の方法はありますか?

ありがとう

4

2 に答える 2

8

PowerShell V3 では Invoke -RestMethod も Invoke-WebRequestヘッダーAccept指定できなかったので、ある程度シミュレートする以下の関数が考えられます。Invoke-RestMethod

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"
于 2015-05-13T14:45:14.157 に答える