【发布时间】:2018-11-23 09:26:22
【问题描述】:
操作系统 - 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 仍然抱怨。有没有人遇到过这个错误?我应该向微软报告吗?感谢您的建议...
【问题讨论】:
标签: rest powershell content-type