【发布时间】:2020-06-21 16:31:24
【问题描述】:
我正在使用 API REST 调用,但问题是由于某种原因它没有正确传递标头值。我收到一个关于它没有从"System.String" 转换为"System.Collections.IDictionary" 的错误。
代码是:
$Headers = New-Object 'System.Collections.Generic.Dictionary[[String],[String]]'
$Headers.Add('X-CENTRIFY-NATIVE-CLIENT', 'true')
$Headers.Add('Content-Type', 'application/json')
$Body = @{
TenantId = 'ID'
User = 'cloudadmin@andrew1.com'
Version = '1.0'
}
#$wr = Invoke-WebRequest -Method Post -Uri $url -Headers $Headers -Body $Body -Verbose
Invoke-RestMethod -Uri "https://uri/Security/StartAuthentication" -Method Post -Headers ($Headers | ConvertTo-Json -Compress) -UseBasicParsing -Body $Body
但是当我执行时,我得到了这个错误(FQID):
Invoke-RestMethod : Cannot bind parameter 'Headers'. Cannot convert the "{
"X-CENTRIFY-NATIVE-CLIENT": "true",
"Content-Type": "application/json"
}" value of type "System.String" to type "System.Collections.IDictionary".
At line:31 char:109
+ ... tication" -Method Post -Headers ($Headers1 | ConvertTo-Json) -UseBas ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
我也尝试过这样的标题:
$headers = @{
'Content-Type'= 'application/json'
'X-CENTRIFY-NATIVE-CLIENT'= 'true'
}
但我仍然遇到同样的错误。奇怪的是它一直在抱怨这个引用。这个库不是 PoSH 原生的。是否有我应该加载的 DLL,或者有更好的方法来解决这个问题?
【问题讨论】:
-
完全删除
ConvertTo-Json,只做-Headers $Headers
标签: json powershell rest