使用 cURL 发布数据时出现“位置参数”错误


"Positional Parameter" error when posting data with cURL

如果我在没有--data "..."的情况下发出命令,它工作得很好。我试过谷歌,但我找不到这个问题的答案。按照此处的说明,当我尝试使用 cURL 发布数据时,我收到以下错误:

PS C:'Users'David> curl --data "SMethod=0" "http://localhost/terra/modules/scripts/Query.php"
Invoke-WebRequest : A positional parameter cannot be found that accepts argument 'SMethod=0'.
At line:1 char:1
+ curl --data "SMethod=0" "http://localhost/terra/modules/scripts/Query.php"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

您的问题在这里得到解答: 在 64 位 Windows 上运行 cURL

你运行的不是 curl,而是在运行一个名为 Invoke-WebRequest 的东西,它别名为 curl。 您需要取消别名 curl,下载并安装 curl(如果还没有)。

Remove-item alias:curl

这将很容易卷曲!!

尝试和乐趣..而且curl也可以自己制作 SHORTEN URL,所以不需要与第三方合作。:D

使用命令提示符而不是PowerShell。

如果您使用的是PowerShell,则需要在curl命令前面加上cmd /c。例如:

cmd /c curl --data "SMethod=0" "http://localhost/terra/modules/scripts/Query.php"

正如其他答案已经提到的,powershell中的curl命令是别名的,并在后台使用Invoke-WebRequest cmdlet。它具有与卷曲类似的功能。

您可以发出POST请求并发送和接收数据,而无需安装任何东西:

curl -body "SMethod=0" "http://localhost/terra/modules/scripts/Query.php" -Method 'POST'

选项 -body (Invoke-WebRequest) 等同于 -d--data (curl)。还必须指定 HTTP 方法。

这个 StackOverflow 答案还考虑了 New-WebServiceProxy 和 Invoke-RestMethod。