在Windows 10 PowerShell中运行Benzinga Curl命令报错求助
问题:Windows 10 PowerShell执行Benzinga API请求报错
我按照Benzinga官方文档操作,尝试运行其提供的示例Curl命令(已替换个人API密钥),整理后的单行命令如下:
curl -X GET 'http://api.benzinga.com/api/v2/calendar/ratings?token=YOUR_TOKEN_HERE&ratings=Upgrades' -H 'accept: application/json'
在Windows 10 PowerShell中执行时,触发如下错误:
Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the "accept: application/json" value of type "System.String" to type "System.Collections.IDictionary". At line:1 char:124 + ... xxxxxxxxxxxxxxxxxxxx&ratings=Upgrades' -H 'accept: application/json' + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
求可正常运行的正确命令语法。
解决方案
Windows PowerShell中curl是Invoke-WebRequest的别名,参数语法和原生Curl不兼容,尤其是请求头的传递方式,以下两种方法均可解决:
方法1:调用原生Curl(推荐)
若你的Windows 10已安装原生Curl(新版本系统默认包含,或通过WSL/单独安装),直接使用curl.exe执行标准Curl命令:
curl.exe -X GET "http://api.benzinga.com/api/v2/calendar/ratings?token=YOUR_TOKEN_HERE&ratings=Upgrades" -H "accept: application/json"
方法2:使用PowerShell原生命令语法
如果要使用PowerShell自带的Web请求命令,需将请求头以字典形式传递:
用Invoke-WebRequest
Invoke-WebRequest -Uri "http://api.benzinga.com/api/v2/calendar/ratings?token=YOUR_TOKEN_HERE&ratings=Upgrades" -Headers @{"accept" = "application/json"} -Method Get
用Invoke-RestMethod(更适合处理JSON响应)
Invoke-RestMethod -Uri "http://api.benzinga.com/api/v2/calendar/ratings?token=YOUR_TOKEN_HERE&ratings=Upgrades" -Headers @{"accept" = "application/json"} -Method Get
内容的提问来源于stack exchange,提问作者user6090578
相关产品推荐
相关产品推荐

