【发布时间】:2022-01-11 16:14:46
【问题描述】:
curl -X POST <myUrl> -H "authorization: Bearer <valid token>"
但是当我发送它时,我得到了异常 - 无法绑定参数“标题”。无法将“System.String”类型的“授权:Bearer”值转换为“System.Collections.IDictionary”类型
【问题讨论】:
标签: powershell http curl
curl -X POST <myUrl> -H "authorization: Bearer <valid token>"
但是当我发送它时,我得到了异常 - 无法绑定参数“标题”。无法将“System.String”类型的“授权:Bearer”值转换为“System.Collections.IDictionary”类型
【问题讨论】:
标签: powershell http curl
curl 是 Windows PowerShell 中 Invoke-WebRequest cmdlet 的别名。
如错误消息所示,该 cmdlet 的-Headers 参数接受标题键值对的字典。
要传递 Authorization 标头,您应该这样做:
Invoke-WebRequest -Uri "<uri goes here>" -Method Post -Headers @{ Authorization = 'Bearer ...' } -UseBasicParsing
(请注意,我明确传递了 -UseBasicParsing 开关 - 如果没有,Windows PowerShell 将尝试使用 Internet Explorer 的 DOM 呈现引擎解析任何 HTML 响应,这在大多数情况下可能不是您想要的)
如果您需要传递名称中带有标记终止字符(如 -)的标头,请使用 ' 引号限定键:
$headers = @{
'Authorization' = 'Bearer ...'
'Content-Type' = 'application/json'
}
Invoke-WebRequest ... -Headers $headers
如果标题 order 很重要,请确保声明字典文字 [ordered]:
$headers = [ordered]@{
'Authorization' = 'Bearer ...'
'Content-Type' = 'application/json'
}
【讨论】: