【问题标题】:How to send curl update request with -d parameter?如何使用 -d 参数发送 curl 更新请求?
【发布时间】:2018-07-07 18:42:08
【问题描述】:

我需要从 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:找不到接受参数'{“name”:“bob”}'的位置参数。 在 G:\IT\bassie\Box\GetUsers.ps1:5 char:1 + curl https://api.box.com/2.0/users/892861590 -H @{"授权" = " ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

我试着把它重新排列成

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

但是错误变成了

Invoke-WebRequest:找不到接受参数“System.Collections.Hashtable”的位置参数。 在 G:\IT\bassie\Box\GetUsers.ps1:5 char:1 + curl https://api.box.com/2.0/users/892861590 -H @{"授权" = " ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

我需要在-d 参数中添加什么?

【问题讨论】:

    标签: powershell curl box-api


    【解决方案1】:

    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 别名。

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

    如果您尝试在 PowerShell 中使用外部工具,那么您必须对他们完全限定 UNC 和名称(包括 externtion),并记住,在 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'

    另请参阅这篇关于尝试在 PowerShell 中使用真正 curl 的帖子。

    如何在PowerShell中使用curl命令?

    我在 PowerShell 中使用 curl 命令在 通过 Jenkins 作业的位桶拉取请求页面。我用了下面 PowerShell 命令来执行 curl 命令,但我得到了 下面提到的错误。谁能帮我解决这个问题 工作了吗?

    How to use the curl command in PowerShell?

    【讨论】:

      【解决方案2】:

      我设法做我需要做的事

      $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 才能使用该格式。

      【讨论】:

        猜你喜欢
        • 2018-06-29
        • 1970-01-01
        • 2018-03-30
        • 1970-01-01
        • 2021-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多