您好我在Powershell v4中运行了以下'Invoke-RestMethed'命令,但它正在抛出HTTP 406错误。
Invoke-RestMethod -Method Post -Uri $url -Headers $head -ContentType "application/xml" -Body $body -OutFile output.txt
我对标题进行了以下更改:
$head = @{"Authorization"="Basic $auth"; "Accept"="*/*"}
我的理解是服务器以xml格式接收请求但是以JSON格式返回,这可能导致问题?我确实尝试将标题更改为“Accept”=“application / json”但收到同样的错误。
完整错误:
Invoke-RestMethod:HTTP状态406 - 类型状态报告 信息 description此请求标识的资源只能生成具有不可接受特征的响应 根据请求“接受”标题。
答案 0 :(得分: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"