Windows 10下cURL调用部分API报错求助
问题:Windows 10下cURL调用部分API报错的原因及解决办法
你遇到的问题其实很典型——在Windows PowerShell环境里,curl这个命令默认是PowerShell自带的Invoke-WebRequest的别名,而不是你手动安装的真正的cURL程序。
场景还原
你调用Coinbase API时的简单GET命令能正常工作,是因为Invoke-WebRequest也支持基础的URL请求,但当你使用cURL特有的参数(比如--header、-F),或者用Linux风格的\换行时,PowerShell就会用自己的规则解析命令,导致各种参数错误。
你的测试命令(Coinbase API):
F:\curl\bin>curl https://api.coinbase.com/v2/prices/spot?currency=USD {"data":{"base":"BTC","currency":"USD","amount":"7268.29"},"warnings": [{"id":"missing_version","message":"Please supply API version (YYYY-MM-DD) as CB-VERSION header","url":"https://developers.coinbase.com/api#versioning"}]}
调用Backtory API的错误命令:
curl -X POST \ --header "X-Backtory-Authentication-Id: 5b0baf0de4b0988b2cea3438" \ --header "X-Backtory-Authentication-Key: 5b0baf0de4b0f80a3dc1d30a" \ -F "username=divar" \ -F "password=123" \ https://api.backtory.com/auth/login
返回的错误信息核心就是PowerShell把curl当成了Invoke-WebRequest,不识别cURL的参数:
F:\curl\bin> curl -X POST \ Invoke-WebRequest : A parameter cannot be found that matches parameter name 'X'. ... PS F:\curl\bin> --header "X-Backtory-Authentication-Id: 5b0baf0de4b0988b2cea3438" \ Missing expression after unary operator '--'. ... PS F:\curl\bin> -F "username=divar" \ -F : The term '-F' is not recognized as the name of a cmdlet, function, script file, or operable program.
解决办法
这里有几个可行的方案,你可以根据自己的习惯选择:
1. 直接调用cURL的完整可执行文件路径
既然你已经把cURL安装在F:\curl\bin,那么直接用完整路径调用curl.exe,就能绕过PowerShell的别名:
F:\curl\bin\curl.exe -X POST ^ --header "X-Backtory-Authentication-Id: 5b0baf0de4b0988b2cea3438" ^ --header "X-Backtory-Authentication-Key: 5b0baf0de4b0f80a3dc1d30a" ^ -F "username=divar" ^ -F "password=123" ^ https://api.backtory.com/auth/login
注意:Windows环境下换行用^而不是Linux的\,这也是你之前命令出错的小细节。
2. 临时取消PowerShell中的curl别名
在当前PowerShell会话中执行以下命令,就能暂时让curl指向你安装的cURL程序:
Remove-Item Alias:curl
这个设置只在当前会话有效,关闭PowerShell后会自动恢复默认的别名。
3. 永久取消curl别名
如果你经常用cURL,不想每次都手动取消别名,可以修改PowerShell的配置文件:
- 先检查配置文件是否存在,不存在就创建:
if (-not (Test-Path $PROFILE)) { New-Item -Path $PROFILE -ItemType File -Force } - 打开配置文件编辑:
notepad $PROFILE - 在文件中添加一行:
Remove-Item Alias:curl -ErrorAction SilentlyContinue - 保存后关闭,下次启动PowerShell时,
curl就会默认调用你安装的cURL程序了。
内容的提问来源于stack exchange,提问作者Mostafa Gholami
相关产品推荐
相关产品推荐

