PowerShell无法识别提供的参数

时间:2018-06-13 17:46:18

标签: rest powershell content-type

操作系统 - Windows 10

Powershell版本 - 5.1.15063.1088

我正在使用 Windows PowerShell ISE (如果这有意义的话)。运行简单的HTTP Post函数时

Function MyBeautifulFunction {

  $myCredentials = Get-Credential

  $body = @{
          "query"="SELECT [System.Wheel],[System.SparkPlug],[System.EngineOil],[System.Headlight],[System.RadioButton] FROM JunkYard WHERE [System.Brand] = 'Ford' AND [System.Model] = 'GT' AND [System.Title] CONTAINS 'Rebuild'"
  } | ConvertTo-Json

  Invoke-RestMethod -Uri 'http://example.com//ford/parts/fuelpump/_apis/wit/wiql?api-version=2.0' `
    -Method Post `
    -Credential $myCredentials `
    -Body $body `
    -ContentType 'application/json' 

}

MyBeautifulFunction

编者注:Invoke-RestMethod调用最初分布在2行而没有续行,下面讨论了Adam's answer部分。

我在PowerShell ISE控制台窗口中收到以下错误消息:

"message":"The request indicated a Content-Type of \"application/x-www-form-urlencoded\" for method type \"POST\" which is not supported. Valid 
content types for this method are: application/json, application/json-patch+json.

我清楚地看到我在参数中传递了'application/json',但由于某种原因,PowerShell仍然抱怨。以前有人遇到过这个错误吗?我应该向微软报告吗?谢谢你的建议...

2 个答案:

答案 0 :(得分:1)

一些事情......

首先,我一直使用application / json的内容类型调用REST服务,我没有任何问题。例如,以下对我来说很好:

$url = 'https://jsonplaceholder.typicode.com/posts/1'
$response_from_webservice = Invoke-RestMethod -Uri $url -ContentType 'application/json' -Method 'Get'

jsonplaceholder ...是一项公共服务,可供测试。如果您需要进行健全性检查,请随意在您的机器上运行该片段。

其次,您发送的示例代码包含此Accept参数:

Invoke-RestMethod -Uri 'http://example.com//ford/parts/fuelpump/_apis/wit/wiql?api-version=2.0' -Method Post -Credential $myCredentials 
-Body $body -ContentType 'application/json' -Accept 'application/json'  

我不相信我以前见过这个。我也使用Powershell v5.1.15063.1088运行Windows 10。我检查了docs。我检查了命令行开关的参数。我没找到。

enter image description here

我分享这个解释,我无法使用该呼叫签名重新创建您的场景。

如果要在HTTP标头中添加accept条目,则必须直接执行此操作:

$h = new-object 'system.collections.generic.dictionary[[string],[string]]'
$h.add('accept', 'application/json')
$r = Invoke-RestMethod -Uri '...' -ContentType 'application/json' -Method 'Post' -Headers $h

第三,看起来你没有在你的代码段中指定内容类型。该片段在body和contenttype参数的单独行上有Invoke-RestMethod。要清楚,它不是包装。看起来那里有一个换行符。这意味着第二行-Body $body -ContentType 'application/json' -Accept 'application/json'与之前的调用无关;以下是您帖子的复制和粘贴:

Invoke-RestMethod -Uri 'http://example.com//ford/parts/fuelpump/_apis/wit/wiql?api-version=2.0' -Method Post -Credential $myCredentials 
-Body $body -ContentType 'application/json' -Accept 'application/json'  

如果你想把参数放在一个单独的行上,只需使用反引号。

Invoke-RestMethod -Uri 'http://example.com//ford/parts/fuelpump/_apis/wit/wiql?api-version=2.0' `
-Method Post `
-Credential $myCredentials `
-Body $body `
-ContentType `
'application/json' 

现在,如果您没有指定ContentType(正如我所建议的那样),它将在帖子上默认为“application / x-www-form-urlencoded”。根据微软的说法:

  

-ContentType

     

指定Web请求的内容类型。

     

如果省略此参数且请求方法为POST,   Invoke-RestMethod将内容类型设置为   应用程序/ x-WWW窗体-urlencoded。否则,内容类型不是   在电话中指定。

答案 1 :(得分:0)

您需要将标头传递到标头参数

$headers = @{
    "Content-Type" = "application/json",
    "Accept" = "application/json"
}

Invoke-RestMethod -Uri 'http://example.com/123' -Method Post -Body $body -Headers $headers 

请参阅 - https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod