如何使用 -d 参数发送 curl 更新请求?

How to send curl update request with -d parameter?

我需要从 powershell 发送一个 curl 请求,使用 box api reference 寻求帮助(我正在查看名为 Update User 的部分,但我遇到了一些问题:

curl https://api.box.com/2.0/users/11111 -H @{"Authorization" = "token"} -d '{"name": "bob"}' -X PUT

应该更新用户名,但我得到:

Invoke-WebRequest : A positional parameter cannot be found that accepts argument '{"name": "bob"}'. At G:\IT\bassie\Box\GetUsers.ps1:5 char:1 + curl https://api.box.com/2.0/users/892861590 -H @{"Authorization" = " ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

我尝试将其重新排列为

-d @{"name" = "bob"} 

但是错误变成了

Invoke-WebRequest : A positional parameter cannot be found that accepts argument 'System.Collections.Hashtable'. At G:\IT\bassie\Box\GetUsers.ps1:5 char:1 + curl https://api.box.com/2.0/users/892861590 -H @{"Authorization" = " ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

我需要在 -d 参数中输入什么?

curl 是 PowerShell - Invoke-WebRequest 的别名,因此会出现错误。

# Get parameters, example, full and Online help for a cmdlet or function

(Get-Command -Name Invoke-WebRequest).Parameters
Get-help -Name Invoke-WebRequest -Examples
Get-help -Name Invoke-WebRequest -Full
Get-help -Name Invoke-WebRequest -Online

Get-Alias -Definition Invoke-WebRequest | Format-Table -AutoSize -Wrap

CommandType Name                      Version Source
----------- ----                      ------- ------
Alias       curl -> Invoke-WebRequest               
Alias       iwr -> Invoke-WebRequest                
Alias       wget -> Invoke-WebRequest    

如果您尝试在 PowerShell 中使用真正的 curl,则必须使用 curl.exe,或者从 Invoke-WebRequest 中删除 curl 别名。

错误是因为传递 parameters/arguments Invoke-WebRequest 不知道它们是什么或如何处理。

如果您尝试在 PowerShell 中使用外部工具,那么您必须完全限定 UNC 和名称(包括外部),并记住在 PowerShell 中使用外部工具,这必须以定义的方式进行。

例如:

请参阅将 Windows PowerShell 用于 运行 旧命令行工具(及其最奇怪的参数) 'https://blogs.technet.microsoft.com/josebda/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters'

另请参阅此 post 关于尝试将真正的 curl 与 PowerShell 结合使用。

How to use the curl command in PowerShell?

Am using the curl command in PowerShell to post the comment in bit-bucket pull request page through a Jenkins job. I used the below PowerShell command to execute the curl command, but am getting the error mentioned below. Could anyone please help me on this to get it worked?

How to use the curl command in PowerShell?

我设法用

完成了我需要的事情
$url = "https://api.box.com/2.0/users/111111111"
$headers = @{"Authorization" = "Bearer TOKEN"}
$body = '{"status": "inactive"}'
$contentType = "application/json"

Invoke-WebRequest $url -Headers $headers -ContentType $contentType -Body $body -Method PUT

所以看来我不仅需要将-D参数替换为-Body,而且我还必须将ConteType指定为application/json才能顺序使用该格式。