如何在PowerShell中使用curl命令?

VAD*_*309 14 powershell curl jenkins

我使用curlPowerShell中的命令通过Jenkins作业在bit-bucket pull请求页面中发布评论.我使用下面的PowerShell命令来执行curl命令,但是我收到了下面提到的错误.有人可以帮我解决这个问题吗?

$CurlArgument="-u xxx@gmail.com:yyyy -X POST https://xxx.bitbucket.org/1.0/repositories/abcd/efg/pull-requests/2229/comments --data content=success"
$CURLEXE='C:\Program Files\Git\mingw64\bin\curl.exe'
& $CURLEXE $CurlArgument
Run Code Online (Sandbox Code Playgroud)

错误详情:

curl.exe : curl: no URL specified!
At line:3 char:1
+ & $CURLEXE $CurlArgument
+ ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (curl: no URL specified!:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

curl: try 'curl --help' or 'curl --manual' for more information

Yug*_*omo 13

或者,您可以使用splatting调用curl.exe ,如下所示。

> curl.exe '-u', 'xxx@gmail.com:yyyy',
 '-X', 'POST',
 'https://xxx.bitbucket.org/1.0/repositories/abcd/efg/pull-requests/2229/comments',                
 '--data', 'content=success'
Run Code Online (Sandbox Code Playgroud)

使用此命令了解curl.exe在哪里Get-Command curl.exe

其他选项是删除别名curl命令Invoke-WebRequest

在 PowerShell 中查看和删除别名

>Get-Aliases
>Remove-Item alias:curl
Run Code Online (Sandbox Code Playgroud)

然后只需运行不带“.exe”的命令

> curl '-u', 'xxx@gmail.com:yyyy',
 '-X', 'POST',
 'https://xxx.bitbucket.org/1.0/repositories/abcd/efg/pull-requests/2229/comments',                
 '--data', 'content=success'

Run Code Online (Sandbox Code Playgroud)

我希望这会有所帮助。


Ans*_*ers 8

使用飞溅

$CurlArgument = '-u', 'xxx@gmail.com:yyyy',
                '-X', 'POST',
                'https://xxx.bitbucket.org/1.0/repositories/abcd/efg/pull-requests/2229/comments',
                '--data', 'content=success'
$CURLEXE = 'C:\Program Files\Git\mingw64\bin\curl.exe'
& $CURLEXE @CurlArgument
Run Code Online (Sandbox Code Playgroud)

  • 那是 `curl` 状态输出。添加参数`-s`来抑制它。 (2认同)

小智 7

在Powershell 3.0及更高版本中,既有Invoke-WebRequest也有Invoke-RestMethod。Curl实际上是PoSH中Invoke-WebRequest的别名。我认为使用本地Powershell比curl更合适,但这取决于您:)。

Invoke-WebRequest MSDN文档位于此处:https ://technet.microsoft.com/zh-cn/library/hh849901.aspx?f=255&MSPPError =-2147217396

Invoke-RestMethod MSDN文档位于此处:https ://technet.microsoft.com/zh-cn/library/hh849971.aspx?f=255&MSPPError=-2147217396

  • 不幸的是,Invoke_WebRequest 在使用 curl 可以做的事情的列表中甚至不接近 curl。简单的 `--verbose` 标志无法查看有关 TLS 连接、RAW http 请求和响应的详细信息。我错过了什么吗? (9认同)

Dic*_*A S 6

PowerShell 现在支持默认别名,如果您键入,help curl
您将得到以下输出

NAME
    Invoke-WebRequest

SYNTAX
    Invoke-WebRequest [-Uri] <uri> [-UseBasicParsing] [-WebSession <WebRequestSession>] [-SessionVariable <string>] [-Credential <pscredential>] [-UseDefaultCredentials] [-CertificateThumbprint <string>] [-Certificate
    <X509Certificate>] [-UserAgent <string>] [-DisableKeepAlive] [-TimeoutSec <int>] [-Headers <IDictionary>] [-MaximumRedirection <int>] [-Method {Default | Get | Head | Post | Put | Delete | Trace | Options | Merge | Patch}] [-Proxy
    <uri>] [-ProxyCredential <pscredential>] [-ProxyUseDefaultCredentials] [-Body <Object>] [-ContentType <string>] [-TransferEncoding {chunked | compress | deflate | gzip | identity}] [-InFile <string>] [-OutFile <string>] [-PassThru]
     [<CommonParameters>]


ALIASES
    iwr
    wget
    curl


REMARKS
    Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
        -- To download and install Help files for the module that includes this cmdlet, use Update-Help.
        -- To view the Help topic for this cmdlet online, type: "Get-Help Invoke-WebRequest -Online" or
           go to https://go.microsoft.com/fwlink/?LinkID=217035.
Run Code Online (Sandbox Code Playgroud)

因此,要下载文件,您可以输入

curl -Uri "https://www.example.com/myfile.txt" -OutFile myfile.txt
Run Code Online (Sandbox Code Playgroud)

  • 请注意,“curl”作为 PowerShell Core 中的别名已被删除。 (2认同)

小智 5

您可以使用以下命令代替curl:

(New-Object System.Net.WebClient).DownloadString("http://google.com")
Run Code Online (Sandbox Code Playgroud)