How to send curl update request with -d parameter?

不问归期 提交于 2020-01-06 06:00:27

问题


I need to send a curl request from powershell, using box api reference for help (I'm looking the the section called Update User, but I'm having some trouble:

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

Should update the user's name, but I get:

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

I tried re-arranging it to

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

but the error changed to

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

What do I need to put into the -d parameter?


回答1:


curl is an alias for PowerShell - Invoke-WebRequest, hence the error.

# 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    

If you are trying to use real curl in PowerShell, then you must use curl.exe, or remove the curl alias from Invoke-WebRequest.

The errors are because passing parameters/arguments that Invoke-WebRequest has no idea what they are or what to do with the.

If you are trying to use external tools in PowerShell, then you have to fully qualify the UNC and name including the externtion, to them and remember that using external tools with PowerShell, this must be approached in a defined way.

For example:

See Using Windows PowerShell to run old command line tools (and their weirdest parameters) 'https://blogs.technet.microsoft.com/josebda/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters'

See also this post regarding trying to use real curl with 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?




回答2:


I managed to do what I needed with

$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

So it seems that not only did I need to replace the -D parameter with -Body, but I also had to specify the ConteType as application/json in order to use that format.



来源:https://stackoverflow.com/questions/48495004/how-to-send-curl-update-request-with-d-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!